Print number Patterns

How can we show output as given below in appian using expression rule?

output:

1

22

333

4444

55555

  Discussion posts and replies are publicly visible

Parents
  • fn!joinarray(fn!repeat(ri!myNumber, ri!myNumber))

    Brief explanation:

    ri!myNumber is the rule input to the expression, of type Integer.

    fn!repeat() generates a list. It takes two parameters: 

    • times
    • input

    so here we are generating a list, the length of which is defined by the value of ri!myNumber, and the value of each item is also ri!myNumber

    Since your output appears to be a a single string, the fn!joinarray is taking the list and collapsing it into a single value.

    If you're actually wanting ALL of the outputs you've defined then you'll need to take the ri!myNumber, generate a list of the numbers from 1 to that number, and then use that list to iterate over the same pattern as expressed in the first code sample, like this:

    a!localVariables(
      local!list: fn!enumerate(ri!myNumber) + 1,
      a!forEach(
        items: local!list,
        expression: fn!joinarray(fn!repeat(fv!item, fv!item))
      )
    )

    Now you have a general purpose Expression which you can pass any number and get the output to conform to the pattern of the example you provided. If you pass the value 5 to the new code you get this output:

    Note: if you need the output to be a list of integer you'll have to use the fn!tointeger() to cast from text to integer.

  • Hi stewart,

    Your solution helps.But what if we have String in that place.

    like 

    A

    BB

    CCC

    DDDD

  • Here you would need to effectively perform a substitution of the number that the previous example has been using with the relevant character:

    a!localVariables(
      local!alphabet: {"A","B","C","D","E"},
      local!list: fn!enumerate(ri!myNumber) + 1,
      a!forEach(
        items: local!list,
        expression: fn!joinarray(fn!repeat(fv!item, fn!index(local!alphabet,fv!item,"?")))
      )
    )

Reply Children
No Data