ksh Array name determined at runtime


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ksh Array name determined at runtime
# 1  
Old 04-16-2013
ksh Array name determined at runtime

I'm working on a ksh script and was wondering if anyone knows if it's possible to access an array by a variable that is determined at runtime?

I have created a test example below, but essentially I will have declared and populated an array (see colorArray below). What I'd like to do is access that array based on a variable value (see tmpVar) that contains the name of the array. See the last line in the code example below.

Is this possible? If so, can you point me in the right direction?


Code:
#!/bin/ksh

typeset -A colorArray

colorArray[color1]=red
colorArray[color2]=blue
colorArray[color3]=green
colorArray[color4]=yellow

echo "Color Array"
for index in "${!colorArray[@]}"; do
    print ">>> $index ${colorArray[$index]}"
done


tmpVar=colorArray
echo "$colorArray"

echo "Array value is ${$colorArray[color1]}"

# 2  
Old 04-16-2013
For a starter, the value in square brackets has to be numeric, so color1 (or even colour1) would be illegal.

Try:-
Code:
#!/bin/ksh

typeset -A colour

colour[1]=red
colour[2]=blue
colour[3]=green
colour[4]=yellow

echo "Colour Array"
for index in "${!colour[@]}"; do
    print ">>> $index ${colour[$index]}"
done


tmpVar=colour
echo "$colour"

echo "Array value is ${colour[1]}"


I hope that this helps.
Robin
Liverpool/Blackburn
UK
# 3  
Old 04-16-2013
Associative arrays are supported in ksh93.
Code:
typeset -A colorArray   # set array type as associative array
# => you can use string as index of array

colorArray["color1"]=red
colorArray["color2"]=blue
colorArray["color3"]=green
colorArray["color4"]=yellow

echo "Color Array"
for index in "${!colorArray[@]}"; do
    print ">>> $index ${colorArray[$index]}"
done



echo "Array value is ${colorArray["color1"]}"

tmpVar=colorArray
echo "$tmpVar"
# eval - parse cmdline twice, some of $ not parsed in 1st parsing => \$
eval value1="\${$tmpVar["color1"]}"
eval value4="\${$tmpVar["color4"]}"
echo "color1 :" $value1
echo "color4 :" $value4


Last edited by kshji; 04-16-2013 at 12:27 PM..
# 4  
Old 04-16-2013
ksh Array name determined at runtime

Perfect. That's exactly what I was looking for. I was playing with eval but couldn't get it working.

Thanks again.
# 5  
Old 04-16-2013
eval is dangerous, avoid it if you can.

Not everything has to be an array all the time, you can loop over strings just as easily -- more easily in fact. This lets you avoid the eval:

Code:
LIST="a b c d e"
NAME="LIST"

for X in ${!NAME}
do
        echo "$X"
done


Last edited by Corona688; 04-17-2013 at 12:40 PM.. Reason: typo
# 6  
Old 04-17-2013
I'm struggling with this. It looks good, but I get:-
Code:
${!NAME}: 0403-011 The specified substitution is not valid for this command.

on AIX 6.1 ksh and on AIX6.1 or RHEL 6.3 I get this for ksh93:-
Code:
NAME

It works in bash, but that wasn't the requirement.

Is there a tweak to get this neater coding to work in ksh?




Robin
# 7  
Old 04-17-2013
Er, I made a mistake in that. It should be NAME="LIST" not NAME="list" since variable names are case sensitive.

It should work fine in ksh93 with that in mind, though if you're stuck with an ancient ksh88 it will not.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash array variables are changed in loop runtime

I am trying to check whether particular host and port are responding or not. I am using below script to check. but node_port array that i am using in loop is getting replaced with previous iteration value. Script and output is given. Please help me to understanding why node_port values are... (5 Replies)
Discussion started by: tmalik79
5 Replies

2. UNIX and Linux Applications

Array in Ksh

Hi Guys, My code is something like this set -A A1 1 7 13 19 set -A A2 2 8 14 20 set -A A3 3 9 15 21 echo "Enter a number" read number for i in 0 2 3 4 do if }" ] then do something elif }" ] then do something elif }" ] then do something (4 Replies)
Discussion started by: jeanzibbin
4 Replies

3. Shell Programming and Scripting

Array in ksh with if-else

Hi All, My Requirement is as follows: 1. User will input Source Sytem Code as input. 2. I have source system codes as 11, 34, 56, 99, 45 etc. OS Version: SunOS 5.8 Generic_117350-62 sun4u sparc SUNW,Sun-Fire-V890 My code is like... echo 'Source System Code: \c' read varSSCode... (3 Replies)
Discussion started by: saps19
3 Replies

4. Shell Programming and Scripting

exit status from ksh script exec from java using runtime

how do i get the exit status from a ksh or perl script executed in a java program using Runtime? (1 Reply)
Discussion started by: twk
1 Replies

5. UNIX for Dummies Questions & Answers

Need help with KSH Array assignment

The following command is only intermittently successful, depends on the data I give it: set -A ImageShifts_sorted `awk '/START_SECTION IMAGE_DEFINITION/ {getline;getline;getline;getline; print $2"-"$3}' temp.ASCII | sort -u` My Error: set: -1: unknown option Finally, If I run that... (3 Replies)
Discussion started by: nerdcurious
3 Replies

6. Shell Programming and Scripting

Perl - Problems iwith colors while printing from dynamic array at runtime..

Hi Perl folks, I am having problems printing elements from an array at runtime. I wish to push elements into array at runtime and the print it later. Now I wish to print this matrix using colors. So I do something like this to enter the runtime values in array: ... (1 Reply)
Discussion started by: som.nitk
1 Replies

7. Shell Programming and Scripting

Ksh array solution.

I was wondering if ksh supported arrays. I have a script that may work with several hosts. I'd like a means of knowing how many hosts I'm working with and an easy way to access them (as variables) in a loop. I'm assuming there's some kind of foreach in shell scripting. (1 Reply)
Discussion started by: mrwatkin
1 Replies

8. Shell Programming and Scripting

using array in ksh

hi all, need help with putting names in an array, i have a few servers which i look up by doing a 'find . -name "*.pid' and the format of the output is like following : ./servername/myserver.pid i was wondering how can i iterate through and store each name in one array my code is... (1 Reply)
Discussion started by: cesarNZ
1 Replies

9. Shell Programming and Scripting

Execute oracle query determined at runtime from a unix script

Hi I am trying to run a SQL statement from a unix script determined at runtime. It is throwing me an error. Please advise some solution to this. echo "Enter username for the database" read username echo "Enter password for the database" read password echo "Enter SQL stmt" read... (4 Replies)
Discussion started by: infyanurag
4 Replies

10. UNIX for Advanced & Expert Users

How is system load determined?

Hello! I'm wondering what factors are used to determine the "system load" Where can i look to get more information on this? Patrick (3 Replies)
Discussion started by: Patrick_Morris
3 Replies
Login or Register to Ask a Question