How to insert separators between columns?

Assuming that @separators has the right number of values, you could use roundrobin.

roundrobin( @columns, @separators ).flat.join()

So this should work for a any length of @columns or @separators :

First we use the >>,>> hyper operator to make a list of lists.

(@columns >>,>> @separators)

Which gives :

[("column1", ","), ("column2", "|"), ("column3", ",")]

Then we flatten this into a single list using a slip.

(@columns >>,>> @separators).map( |* )

Which gives:

("column1", ",", "column2", "|", "column3", ",").Seq

Then we get the array of all but the last value :

(@columns >>,>> @separators).map(|*).head(*-1)

For :

("column1", ",", "column2", "|", "column3")

And finally join it with nothing :

(@columns >>,>> @separators).map(|*).head(*-1).join("")

Final result :

column1,column2|column3

Changing the number of columns or separators will not make a difference.

Tags:

Raku