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!

Using Variable as Array Name 1

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
0
0
DE
Hi folks,

usually I create certain arrays using the following command:

set -A Array_Name $(cat /list.lst)

Now I'd like to change the command so that the array name is taken from a variable.

So far I e.g. tried the following:

Array_Name=$(echo "Test")
set -A $Array_Name $(cat /list.lst)

Problem is when i try to see what's in my array using

echo ${Array_Name[*]}

The output looks like this:

Test


...

What's wrong here ?

Regards
Thomas
 
Try this:
eval echo \${$Array_Name[*]}

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That did it !

Thanks a lot :)
 
If you need access to array elements, here is some code going that way
Code:
#! /bin/ksh
for ARRAY in Tab1 Tab2
do
        # some Array initialisation
        case $ARRAY in
                Tab1) eval set -A $ARRAY aaa bbb ccc ddd eee ;;
                Tab2) eval set -A $ARRAY 111 222 333 444 555 666 ;;
        esac

        # construct array size sentence
        TAB=$(echo \${#${ARRAY}[\*]})
        print "size of array $ARRAY = $TAB = \c"
        # evaluate array size
        SIZE=$(eval echo $TAB)
        print "$SIZE , content follows"
        # some array usage
        I=0
        while [ "$I" -lt "$SIZE" ]
        do
                # construct some pointer to array
                PTR=$(echo \${$ARRAY[$I]})
                print "$PTR = \c"
                #evaluate the result of pointer
                eval echo $PTR
                # go ahead
                (( I = I + 1 ))
        done
done
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top