K
K
ksvdon2014-10-20 20:53:40
bash
ksvdon, 2014-10-20 20:53:40

How to loop through arrays in bash?

I have some text. Log of some values. I break this text into elements and throw it into an array for easy comparison with readings from other sources. I want to be able to create several arrays in a loop so as not to select by time, but simply drive all the values ​​into different arrays for a certain period and compare with the standard. I tried to stick a variable into the array name (a number that can be increased in a loop and in such a way that another array would be created each time) of the type: MASS[0]=`что-то там`
you simply p=0 MASS$p[0]=`что-то там`cannot insert a variable. Here is the question. How can you?
I look at how many suitable strings will go into arrays; in the while []; do loop I want to create as many arrays as the number of lines the script calculates.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
3
3vi1_0n3, 2014-10-21
@3vi1_0n3

I can suggest a better option. Don't create multiple arrays, just use arithmetic to calculate the index.

#!/bin/bash

for i in {1..5}
do
    for j in {1..5}
    do
        INDEX=$((i*1000+j))
        ARRAY[INDEX]=$j
    done
done
echo Full array:
echo ${ARRAY[*]}
echo First line:
for j in {1..5}
do
    INDEX=$((1000+j))
    echo ${ARRAY[INDEX]}
done

There will be only one inconvenience in this case - it will be necessary to know in advance the maximum number of elements in the "line". In the example, the index shift is 1000

V
Vladimir, 2014-10-21
@rostel

something like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question