Array reference problem

i have a variable MYHOST that has my host name.depending on the host i have an array like A_<hostname>.Everytime i need to append the hostname to A_ to get the array.but in the shell script i am nt able to access the members of that array.

code of what i hav done:

export temp=A_$MYHOST
for d in $((temp))
do
...
...
...
done

now i get an arithmetic syntax error in the statement :"for d in $((temp))"

thanks in advance

for d in $temp
do
    # your code here
done

Try that

If variable temp contains the name of your array, I think you want to expand it like this:

for d in "${!temp[@]}"

Each element of the array named by temp will be quoted.

The code as written by the OP creates a "normal" environment variable, so simply writing $temp allows the for loop to work with each word.

@jim

im not able to refer still getting the same error...

@....
for d in "${!temp[@]}"

still gets only a value 0 only and not the array values....

A_<hostname>=(1 2 3)
temp has A_<hostname> as its value....i now want to get the values from A_<hostname> array....

can you tell us the output of below comands.

echo $MYHOST
echo $A_MYHOST

$MYHOST
tragedy

$A_MYHOST

no value is displayed

niteesh that just means your MYHOST variable contains only 1 value namely "tragedy".

so your loop will only take 1 value as it contains only 1 value. if your variable has more than 1 value then your for loop will do more iterations.

for d in $temp #this temp has only 1value , the loop will run only one time
do 
your code
done

@dazdseg

actually see the situation is this....i agree that myhost has only one value but see the value of temp is

A_<the value in the myhost>
ie A_tragedy
A_tragedy is a predefined array now i need to access members of it

i am not sure, that i am actually getting what you are saying. you mean to say A_tragedy is a pre defined array.
can you show me the input of what exactly your array A_tragedy contains??
As in what are the input members and what is the exact thing you are looking for as an output.

export A_tragedy="1 2 3"
$MYHOST has tragedy
temp has value A_tragedy

now i want to access the members of array A_tragedy using the variable temp and print the array.

thanks in advance

echo $MYHOST >> TEMP
for word in ${TEMP}
do 
##your code 
done

try this and see if its working because as you are saying it should work

temp is a variable...i append content of $MYHOST to "A_" to get the name of the array,which i store in temp
so,

temp has A_tragedy....ie temp ="A_" appending to $MYHOST
$MYHOST=tragedy

You will have to use "eval", because variable expansion is done in one step in the shell and you need a two-time expansion: you need to expand "A_$MYHOST" to "A_HOSTNAME" and then "$A_HOSTNAME[<nr>]" to the value. See this post for a detailed explanation.

The following example code should tell you what you need:

typeset hostname="myserver"
typeset -i a_myserver[1]=1
typeset -i a_myserver[2]=2
typeset -i a_myserver[3]=3
typeset -i a_myserver[4]=4
typeset -i a_myserver[5]=5

# Display one value, showing the difference between "\\" and "\":
eval echo \\$a_\$hostname[1] is \${a_$hostname[1]}

# Cycle through all elements of the array: and store to a different array NewArr[]:

typeset iCnt=1
while [ $iCnt -le $(eval echo \${#$hostname[*]}) ] ; do
    typeset NewArr[$iCnt]=$(eval echo \${$hostname[$iCnt]})
    (( iCnt += 1 ))
done

I hope this helps.

bakunin

@bakunin

its working...thank you soo much....do u mind just explaining me what is happening with the command \${#$hostname[*]})

This tells you the number of elements in an array. I use it to end the loop at the appropriate array index. For example:

typeset array[1]="x"
typeset array[2]="y"
typeset array[3]="z"

echo ${#array[*]}     # gives "3" as "array[]" contains 3 elements

typeset array[4]="u"

echo ${#array[*]}     # gives now "4"

# like in the sample code above we introduce a further level of indirection:

typeset arr_name="array"

eval echo \${#$arr_name[*]}   # will also yield "4"

You might see "${#array[@]}" instead of "${#array[*]}". Both these expressions will evaluate to the same value - the number of elements stored in the array.

I hope this helps.

bakunin