0

I wonder , if we can save two values , for example , name and number , in the same array member , for example , i have write the following code to do this :

`array[$count]={$x , 1}`

where $x containe a string , but when i want to print the value that array[$count] have :

`echo "$count - $x1 - ${array[$count]} \n"`

it's give just the first value which is $x

1 Answer 1

1

bash does not have multidimensional arrays, but you can fake it using associative arrays:

$ declare -A array
$ count=5
$ array[$count,name]="foobar"
$ array[$count,value]=1
$ for idx in "${!array[@]}"; do printf "%s\t%s\n" "$idx" "${array[$idx]}"; done
5,value 1
5,name  foobar

This requires bash version 4

Sign up to request clarification or add additional context in comments.

10 Comments

does associative array is similar to hash in perl ? and another question , can i change 'value' , since it should not be a fixed value ?
Yes, associative array is like a hash. But it can only store strings as values. You don't want to change the word "value" in the array index, you want to change the actual value (where I have "1").
okay "value" , what will be , in another word , what will be assign ?
whatever is on the right-hand side of = will be assigned to the variable named on the left-hand side
but what will happen if we assign a value with key that already exist ?
|

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.