Is there a simple/straightforward way to “fold” multiple array elements into one element based on the value of the elements?
For example, given this array:
a = "item_1_green; item_1_blue; item_2_green; item_2_blue;item_2_red;item_3_green;item_4_blue;item_4_red"
where the element separator “;”.
I’d like a new array, b, that contains this:
b = "item_1_green,item_1_blue;item_2_green,item_2_blue,item_2_red;item_3_green;item_4_blue,item_4_red"
where the element separator is still “;”, but the former items are joined with a “,”.
IOW, I’d like to put all elements that have “1” in them into a single element with a new separator, “,”. Then add all elements with “2”; then “3”, etc.
I can probably “brute-force” it with a loop, but I’ve seen such elegant solutions from you guys, that I thought I’d ask here first.