Associative arrays

Hi all,

#!/usr/dt/bin/dtksh
typeset -A wavelength

wavelength["red"]=650
wavelength["orange"]=590
wavelength["green"]=510
wavelength["blue"]=475
wavelength["indigo"]=445
wavelength["violet"]=400

I have created an associative array like the one above.

Now I am trying to print the values

If i give print ${wavelength[violet]} it is working fine.

But my requirement is to store the array name in a variable and pass on to the print statement

like this;
var="wavelength"
print ${$var[violet]}

this is throwing error saying bad substitution.

Please help me out

This might be a simple referencing issue, perhaps the shell is looking for an array called 'var' rather than your variable's value. Have you tried to explicitly dereference 'var'? :

print ${${var}[violet]}

If that works, you might be able to clean things up a bit more.

eval print \${$var[violet]}

works for me.

You need always parse line twice if you like to use variable which value is in variable.
eval is answer = parse command line twice

simple example:

varname=var1
var1=100
eval print \$$varname
#1st parse give command line to run: print $var1
#2nd parse give command line to run: print 100

Thanks folks!!!!:b:

I used eval print \${$var[violet]} and it is working fine!:b: