Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with Associative Arrays in a korn shell script

Status
Not open for further replies.

greyone

Programmer
Dec 14, 2000
200
CA
I have this script in which i ned to store employee codes in an array. So i do this using associative arrays as follows

EmpArr["AD"]="Administration"
EmpArr["MK"]="Marketing"
EmpArr["AC"]="Accounting"

the problem is when i try to extract the employee code description depending on the user input the array seems always read the last a value that is accounting. i do the following to gather the user input and then check to see using multiple if statements:

print "Enter employee code "
read empcode

If [[ $empcode == "AD" ]]
then
print "the employee code description is ${EmpArr[$empcode]}"
elif [[ $empcode == "AC" ]]
then
print "the employee code description is ${EmpArr[$empcode]}"
elif [[ $empcode == "MK" ]]
print "the employee code description is ${EmpArr[$empcode]}"
else
print "Have anice day !"
fi

Am i doing something wrong here. I'm new to UNIX so pardon me if this a very stupid question. But i really can't figure out what's wrong

Please help


 
I didn't know you could use associative arrays in a korn script... Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
One problem I see is the '==' should be '=' in the if and elif statements.
 
Well that did'nt work guys. Is there any other way i should declare the arrays since i feel that there is aproblm with the way i'm decalring it.

Please help



 
not all "ksh" support "associative arrays", i.e ksh in Compaq Tru64 Unix 5.0 supports "associative arrays", but Solaris 8, doesn't, your "associative arrays" declaration is correct, on Solaris it works using "bash".

Hope this helps.

Carlos Almeida.
 
i'm running red hat linux release 7.0 and i guess it does support the associative array otherwise i would have got an error. But my problem is the way i'm declaring the arrays. I just want to know if that is corect and if it's not what is the alternative.


Please Help


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top