Combining lists

Hello everybody.
My operating system is Fedora30, shell - bash
I faced combining lists. I will be glad for help regarding strings, arrays and so on.
The bottom line is as follows. It is necessary to combine each element from the first list with elements from the second.
if the second is longer than the first then need to discard excess.
If the second is less than the first, add elements in a circular.
That is, to pair them in this way
first list:

a b c d i f gg

second list:

1 2 13

desire:

a1 b2 c13 d1 i2 f13 gg1

What conclusion does not matter. May be so:

a 1
b 2
c 13
...

With any separator:

a1,b2,c13,...,gg1

Since the parameters were passed to the function and it was possible to calculate the length of the first list in advance,
the solution was to make the second list redundant and cut off the necessary from the head

arr=(a b c d i f gg) #arr=($1)
paste <(fmt -1 <<<"a b c d i f gg") <(printf %.s'1 2 13 ' $(seq ${#arr[@]}) | fmt -1 | head -${#arr[@]})

Or a more accurate solution.

paste <(fmt -1 <<<"a b c d i f gg") <(yes $'1\n2\n13' | head -${#arr[@]})

Moreover, it is not difficult to convert a variable into functions yes "${2// /$'\n'}"
On the fly from the command line, it will be harder to do!
It seems to me that the solution should be very simple and I'm missing something
I am also interested in decisions regarding single-character lists. I would be grateful for all the solutions.
Thanks everyone

How about (not THORUOGHLY tested!):

$ L1=( a b c d i f gg )
$ L2=( 1 2 13 )
$ for i in ${!L1[@]}; do echo ${L1}${L2[i%${#L2[@]}]}; done
a1
b2
c13
d1
i2
f13
gg1

Pipe through paste -sd" " to get your desired result.

2 Likes

Yes, it's very convenient to insert any separator character or even string,
and if I put in quotation marks then any whitespace characters without "paste"

for i in ${!L1[@]}; do echo ${L1}.....${L2[i%${#L2[@]}]}; done

Thanks

Slightly more "traditional" way of doing this:

for (( i=0; i<${#L1[@]}; i++ ))
do
   printf "%s %s\n" "${L1}" "${L2[i%${#L2[@]}]}"
done

or

lenL1=${#L1[@]} lenL2=${#L2[@]}
for (( i=0; i<lenL1; i++ ))
do
   printf "%s %s\n" "${L1}" "${L2[i%lenL2]]}"
done
1 Like

Yes, it's cool.
"${L2[i%lenL2]]}"
This algorithm can be organized even on "awk" or "bc" with a large size of values
Thanks @Scrutinizer