ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
it looks like you want to use some sort of associative array or dictionary. However to provide help we will need to know what language you are using (it seems to not be one I'm familiar with, since I can't tell from the code you posted).
Hi looks like you may have forgot to declare it as an associative array.
Try the following:
Code:
declare -A subnet_array
subnet_array=([1.1]=US [5.2]=EU [2.3]=JPN)
echo ${subnet_array["1.1"]}
HTH,
Evo2.
PS. I'd never used associative arrays in bash before, and found this
in a linux journal article that was returned as the top hit searching duckduckgo for "bash associative array": http://duckduckgo.com/?q=bash%20associative%20array
Thanks for the info....But I hit another problem...How do I reference it if the index is a variable?
I try using double quote, single quote but all not valid...Below is a simple code for iterate the subnet_array define previous and print out the index and the elements...It fails...Any idea?
###
echo "Array items and indexes:"
for index in ${!subnet_array[*]}
do
echo $index
printf "%4d: %s\n" $index ${subnet_array["$index"]}
done
###
Note that standard arrays use only integers as indexes. Since the "[]" field operates in a mathematic context they can have arithmetic operations directly performed on them, and variables that contain integers don't have to (but can) be prefixed by "$".
Code:
array=( foo bar baz )
for i in 0 1 2 ; do
echo "Array index $i is ${array[i]}, and the next one is ${array[i+1]:-empty}"
done
#output:
Array index 0 is foo, and the next one is bar
Array index 1 is bar, and the next one is baz
Array index 2 is baz, and the next one is empty
Associative arrays, as used here, treat the indexes as text strings, and so any combinations of characters can be used. Since the "[]" field does not operate in an arithmetic environment all variables must have "$" in front of them in order for them to expand.
Associative arrays have to be explicitly declared before you can use them as such. You also can't guarantee what order the indexes will come out in when you use "*" or "@", as they are stored according to the shell's internal memory mapping.
Code:
declare -A array
array=( [one]=foo [dos]=bar [san]=baz )
for i in "${!array[@]}" ; do
echo "Array index $i is ${array[$i]}"
echo "I won't know what the next one is, if any, until the next iteration of the loop"
done
#output:
Array index dos is bar
I won't know what the next one is, if any, until the next iteration of the loop
Array index one is foo
I won't know what the next one is, if any, until the next iteration of the loop
Array index san is baz
I won't know what the next one is, if any, until the next iteration of the loop
A thought that occurs to me also is ... "it's not such a good idea to use Bash as a programming language, even if you 'can.' "
In Unix/Linux, you can write "a command" in any language you please, and no one will be the wiser. But these various languages have one over-arching advantage that Bash does not: they were designed to be "programming languages." In my opinion, the only Unix shell that ever had a true "built-in programming language," as its designer's expressed intention, was and is the Korn shell.
I'm pragmatic and lazy. When I want to write "a computer program," I want to use a tool built for that job, and I want it to let me do as much as possible while writing as little "new, original" code as possible.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.