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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Indirect - Dynamic Array names

Status
Not open for further replies.

gamerland

Programmer
Apr 6, 2003
53
0
0
US
I have a KSH function and I have simplified the issue. If too much let me know.

funcPrintList
{
NAMELIST=$1
for NAME in $NAMELIST
do
print $NAME
done
return 0
}
#
# Array of names
#
set -A NMELIST bob jane rick sarah
#
#mainline
#
funcPrintList "${NMELIST[*]}"
exit 0

This works fine for an Array that I know the name of. However, I would like to be able to process the name dynamically, like:
set -A ABCLIST bob jane rick sarah
set -A XYZLIST jane rick sarah
set -A EFGLIST rick sarah

If I dynamically get the first 3 digits:
FIRST3="ABC"
ARRAYNAME="$FIRST3" "LIST"
Now $ARRAYNAME="ABCLIST"
However, I cannot seem to get the call to the function to work. It ends up sending the name ABCLIST instead of the array.

As always--- Thanks in advance.

 
Try this...
Code:
#!/bin/ksh

set -A ABCLIST  bob jane rick sara
set -A XYZLIST  jane rick sarah
set -A EFGLIST  rick sarah

FIRST3="ABC"
ARRAYNAME=${FIRST3}LIST
print "Which array: ${ARRAYNAME}"
eval print \"Array values: \$\{${ARRAYNAME}[*]\}\"

eval funcPrintList \"\$\{${ARRAYNAME}[*]\}\"
The [tt]eval[/tt] command causes the line to be parsed twice. That's why the backslash character. You backslash, or escape, each of the characters you don't want parsed on the first pass. This causes the first parsing to change the function call line to...
Code:
funcPrintList "${ABCLIST[*]}"
...which is then executed. I believe this is what you want.

Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top