问题描述:

I am trying to merge two arrays into one in a zipper like fashion. I have difficulty to make that happen.

array1=(one three five seven)

array2=(two four six eight)

I have tried with nested for-loops but can't figure it out. I don't want the output to be 13572468 but 12345678.

The actual script I am working on is here (http://ix.io/iZR).. but it is obviously not working as intended. I either get the whole of array2 printed (ex. 124683) or just the first index like if the loop didn't work (ex. 12325272).

So how do I get the output:

one two three four five six seven eight

with above two arrays?

Edit: I was able to solve it with two for-loops and paste (http://ix.io/iZU). It would still be interesting to see if someone have a better solution. So if you have time please take a look.

网友答案:

Assuming both arrays are the same size,

unset result
for (( i=0; i<${#array1[*]}; ++i)); do result+=( ${array1[$i]} ${array2[$i]} ); done
网友答案:

I've found more common the case where I want to zip two arrays into two columns. This isn't as natively zsh at the "RTLinuxSW" answer, but for this case, I use paste. % tabs 16 % paste <(print -l $array1) <(print -l $array2) one two three four five six seven eight

And then can shove that into another array to get the intended output: % array3=( `!!`«tab»«tab» ) % print $array3

相关阅读:
Top