Storing Array values to a single variable

Hi Community,
Suppose if i have one array for example {List1, List2, List3,List4}
I need to loop through it and instead of showing output of array one by one i need to store the each output value to one variable and show it in a message box like
“List1,List2,List3,List 4” please help to find the solution

1 Like

StringJoin(",", YourArrayWithLists.SelectMany(Function (x) x))

Hi @alan.prakash

If you have stored all the variables List1, List2, List3 and List4 which have values in it.
Then you want to print all the values in array variable. Let’s call array variable name as Arr_Items.

Use the below expression to print,

String.Join(",", Arr_Items)

Hope it helps!!

In addition to above:
grafik
And as mentioned above
grafik

@alan.prakash

Assign: resultString = String.Join(",", listArray.SelectMany(Function(list) list))


Let the array is of type ‘String’ (Array of String).

Initialize an output string, let’s say ‘outString’

You can just use a for each activity to loop through each item in the array.

Inside the For Each loop, use an Assign activity to concatenate each element to the output variable by using the expression the expression outputString = outputString + item + ",".

After(outside) the loop, use an Assign activity to remove the trailing comma using outputString = outputString.TrimEnd(","c).

Finally, Use a Message Box activity to display outputString.

Regards:)

This will work if the List1, List2, List3 and List4 are string datatype variables… @alan.prakash

If those variables are List datatype variables you can use the @ppr code with select many function or

Try the below expression,

String.Join(", ", Arr_Items.Select(Function(list) String.Join(", ", list)).ToArray())

Explanation -
Arr_Items.Select(Function(list) list) - Iterates through each list variable in the Arr_Items array variable.
String.Join(“,” list).toarray() - Joins the items in each list variable with a comma and stores the result in an array.
String.Join(", “, Arr_Items.Select(Function(list) String.Join(”, ", list)).ToArray()) - Joins the array of concatenated list items with commas, producing a single string where all items from all lists are separated by commas.

image

Based on your requirement use the expressions.

Hope you understand!!

If it is an array of lists, then just use a for each loop inside the for each loop (Nested loop), one looping through each list and inside for each looping through each item in the list.

Rest everything will be the same.

Regards:)

Hi @alan.prakash

Try this in message box


String.Join(",", ArrayVar)


Your value will be display as comma separated, you can change according to your preference like " ", "-", "|"

Hope it helps !!

I need to use for each loop so each current item in a loop should append to it thats my case
Suppose these are my array items{List1,List2,List3} i need to loop through it and inside loop i need to create a variable like output and it should be appended one by one

Just share some sample values and a more elaborated overall-goal description

We shared the String.Join(…

For this we shared with you the SelectMany Part:

YourArrayWithLists.SelectMany(Function (x) x)

We can merge all values into a list/array by single assign without for each

Assign Activity:
arrAllValues = YourArrayWithLists.SelectMany(Function (x) x).toArray

I have attached one screenshot


i have used your logic but throwing error
my use case is it need to create a string variable inside this loop like str_output during the first iteration the value should be List1 in 2nd iteration value should be List1List2 like that, outside the for each loop i need to display that message as List1List2List3 During each iteration i need to concatinate the value

@alan.prakash
dont mix up two things:

  • flat string
  • merged collection
    But for both, we gave the approaches:

And when during a loop the flat string is needed for the looped list then similar you can do:
Arr_SL = String.Join(",",arr_List)

also you can do inspections / RnDs and Prototyping by using the debugging panels e.g. immediate panel:
Understanding the 6 Debugging Panels of UiPath in the easiest way possible! - News / Tutorials - UiPath Community Forum

When the need is really about:

  • looping
  • creating / concatenating looped list values to preivous looped values then we can do:

Variables:
arrAllValues = String Array, Default Value: {}
strFlatString
For each Activity | item in {List1,List2,List3} | Type Argument: String List

  • Log Message: String.Join(",", item)
  • Assign activity: arrValues = arrValues.Concat(item).toArray
  • Assign Activity: strFlatString = strFlatString & "," & String.Join(",", item)

@alan.prakash

Then Try this in you Assign Activity in you For Each Loop

output = IF(output = "", item, output + ", " + item)

Hope it helps !!
Happy Automation :uipath:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.