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.

Reply
  • 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.

Children