cancel
Showing results for
Did you mean:

## Break a number into array of digits

hi all,

I am trying to break a number into an array of digits using split function but it keeps giving the whole number as the first item of the array.

How can this be broken down into array items for each diigt?

Split function used, EmployeeID contains the number 789545

split(split(items('Apply_to_each_2')?['EmployeeID'],'')[0],'')

thanks

5 REPLIES 5
Multi Super User

Hey @aarnav

I have made a flow to get all digits of a number in an array. Its a little long, I hope you understand it.

Heres how the input looks like:

Here is the Output:

Variable A to store the number, variable C to store the digits, Variable D for index number, Variable E just stored variable A's temporary value. And also the condition for do until is given.

Inside Do Until: I am taking last digit of integer. And putting it in array. Its a reverse array.

Append to Array Value: mod(variables('E'),10)

Compose Value: div(variables('E'),10)

Set Variable: outputs('Compose')

Variables F to store reverse array(The result you want).

Variable G: sub(length(variables('C')),1)

Apply to each loop, to get the final values:

Compose 2: variables('C')[variables('G')]

Append to Array 2: outputs('Compose_2')

Last compose to show the output:

I hope you like the solution. If you have any doubts just comment.

Community Champion

Try to use this:

``split(trim(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(variables('strDigits'),'0',' 0'),'1',' 1'),'2',' 2'),'3',' 3'),'4',' 4'),'5',' 5'),'6',' 6'),'7',' 7'),'8',' 8'),'9',' 9')),' ')``

________________________________________________________

Super User

This should do what you want:

The output generated from the select action is:

``````[
"7",
"8",
"9",
"5",
"4",
"5"
]``````

If you want to understand better how this works, this blog post I wrote should help:

The subject of the post isn't what you want to do, but it uses the same technique.

Blog: tachytelic.net

Community Champion

Really elegant approach, @Paulie78. Bravo!

________________________________________________________

Super User

Thanks @VictorIvanidze I love the range function, useful in so many situations.

Announcements