Home > Software design >  how to get index, out of bash array in dictionary
how to get index, out of bash array in dictionary

Time:11-11

I have this code -->

list=("kek" "lol")
dict =(["memes"]=$list)

with array, and the dictionary ( i guess ).
now i want to get, for example, second index out of the list, but using dictionary.
Something like echo ${dict[1]}, but that does not print out anything, even tho if to call echo ${dict[0]}, it will print out kek. So my guess that i have done something wrong on declaring dictionary step... and i was not able to google anything about this issue for some reason.
So... How do i do it?

CodePudding user response:

The value of an associative array is always a scalar. It can't be an indexed array. You could store instead the name of the array (list) into the dictionary and use a nameref to access the array:

list=(kek lol)
dict =([memes]=list) # Store name of array
declare -n plist=${dict[memes]} # Fetch the list
echo ${plist[0]} # Outputs kek

CodePudding user response:

Nvm, you can do it with just declaring the dictionary this way dict =(["memes"]="kek" "lol") , and it will work, still interesting, if it even possible to declare it via variable

  • Related