1

please help me. I have string (all times changing, not constant) like this :

id1 name1 key1
id2 name2 key2
id3 name3 key3
...

how using bash i can put's this values to two-dimensional array?

in result for example having :

array[1][1] -> id1
array[1][2] -> name1
array[1][3] -> key1
array[2][1] -> id2
array[2][2] -> name2
...

TY for the help

2
  • This is not three-dimensional array, it is 2-d array. it is nx3 matrix where n is number of rows... 3-d array would look like array[1][1][1]... Commented Jan 28, 2015 at 13:24
  • I just updated my answer with code. Commented Jan 28, 2015 at 13:42

1 Answer 1

1

Bash does not support multidimensional arrays.
You can simulate them for example with hashes, but need care about the leading zeroes and many other things.

for example :

var="id1 name1 key1"

declare -A matrix
num_rows=1
num_columns=3

read -a array <<< ${var}

matrix[1,1]=${array[0]}
matrix[1,2]=${array[1]}
matrix[1,3]=${array[2]}


for ((j=1;j<=num_columns;j++)) do
    for ((i=1;i<=num_rows;i++)) do
        echo ${matrix[$i,$j]}
    done    
done
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.