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!

KSH Array reference 1

Status
Not open for further replies.

AnotherAlan

Technical User
Feb 10, 2006
362
GB
Here’s what I’ve got as an example.

Set an array called ‘letters’ containing a, b and c.

Then run a for loop setting 3 arrays (a, b and c) to each contain word1, word2 and word3.

All works this far (you can check it with ‘set’ to see that it’s being defined. However, if you then want to do something with the array from within the for loop, I can’t use ${s[*]} as it thinks the array is called ‘s’. If I replace with $s, I get a syntax error. If I replace with ‘s’ with ‘a’, the contents of array ‘a’ are correctly displayed.

Line of death is "echo ${s[*]}"
#!/bin/ksh

set -A letters a b c

getorphans() {

for s in ${letters[*]} ; do

set -A $s word1 word2 word3

echo ${s[*]}

done

}

All help appreciated.

Thanks
Alan
 
for s in ${letters[*]}; do
set -A $s word1 word2 word3
eval echo $s = \${$s[*]}
done


Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
PHV, I have known for a long time that you are a genius.
But can you explain to me why I needed to do the escaping. i.e the \$.
I tried every combination of quotes, brackets and all the ksh particulars. I just don't understand why I am not seeing what you are.

Thanks very much,
Alan
 
In your shell man page have a look at the eval command.
When $s=a the order of execution is:
Typed: eval echo $s = \${$s[*]}
Read by eval: echo a = ${a[*]}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Thanks for your help PHV, much appreciated.

Alan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top