Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop over two array values at a time

Tags:

bash

shell

How can I loop over two array values at a time? I tried using a for loop but I could only figure out how to echo one at a time.

#!/bin/bash

array=(value1 value2 value3 value4 value5 value6 value7 value8 value9 value10)

for i in ${array[@]}
do
        echo $i
done

Is there a way to change the for loop that it will echo two values at a time like below?

value1 value2
value3 value4
value5 value6
value7 value8
value9 value10
like image 646
NewUser Avatar asked Mar 09 '21 21:03

NewUser


People also ask

How do I loop through two arrays at the same time?

To use forEach to loop through two arrays at the same time in JavaScript, we can use the index parameter of the forEach callback to get the element with the same index from the 2nd array. const n = [1, 2, 3, 5, 7, 8, 9, 11, 12, 13]; const m = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; n.

How do you add two arrays to a for loop?

Simply copy the largest array into new array , and replace each index of new array with addition of respective indexes of two arrays . So that index out of range and copying array of larger array into new can be avoided.

Can you loop an array?

The Basic For LoopJavaScript arrays are zero based, which means the first item is referenced with an index of 0. As you can see the for loop statement uses three expressions: the initialization, the condition, and the final expression. The final expression is executed at the end of each loop execution.

How do I loop through an array of elements?

You can loop through the array elements with the for loop, and use the length property to specify how many times the loop should run. The following example outputs all elements in the cars array: There is also a " for-each " loop, which is used exclusively to loop through elements in arrays: for (type variable : arrayname) { ...

How do you iterate through multiple arrays simultaneously?

Loop over multiple arrays simultaneously. Loop over multiple arrays (or lists or tuples or whatever they're called in your language) and display the i th element of each. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop.

What happens if two arrays have different lengths?

If they have different lengths there are two cases: a() is the shortest one: Only elements up to maximum index of a() are printed a() is bigger than another one: if exceeding index to much, program crashes, else it may work because there is some "free space" after end of assigned array memory.

How do I use the loop function?

# The Loop function will apply some function to every tuple built by taking # the i-th element of each list. If one of them is exhausted before the others, # the loop continues at its begining. Only the longests lists will be precessed only once. Loop := function(a, f) local i, j, m, n, v; n := Length(a); v := List(a, Length); m := Maximum(v);


1 Answers

Looping over indices will be easier than looping over the elements. You can pull out the two elements by index:

for ((i = 0; i < ${#array[@]}; i += 2)); do
    echo "${array[i+0]} ${array[i+1]}"
done

Or you could extract array slices using the syntax ${variable[@]:offset:length}:

for ((i = 0; i < ${#array[@]}; i += 2)); do
    echo "${array[@]:i:2}"
done

This would be especially useful if you wanted more than two elements at a time.

like image 157
John Kugelman Avatar answered Oct 20 '22 05:10

John Kugelman