Array reference problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array reference problem
# 8  
Old 07-19-2010
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.

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

# 9  
Old 07-19-2010
@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
# 10  
Old 07-19-2010
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.
# 11  
Old 07-19-2010
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
# 12  
Old 07-19-2010
Code:
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
# 13  
Old 07-19-2010
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
# 14  
Old 07-19-2010
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:

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to reference 2 dimensional array in awk?

Hello, all For a 1-dimensional array, such as myarr_1=1 myarr_1=2 myarr_1=3I know I can write a loop as below to show the array member one by one: for (i in myarr_1){print i, myarr_1}Now, suppose I have a two dimensional array such as: myarray_2=1 myarray_2=2 myarray_2=10 myarray_2=20My... (3 Replies)
Discussion started by: littlewenwen
3 Replies

2. Shell Programming and Scripting

Perl de-reference code reference variable

Guys, May i know how can we de reference the code reference variable.? my $a = sub{$a=shift;$b=shift;print "SUM:",($a+$b),"\n";}; print $a->(4,5); How can we print the whole function ? Please suggest me regarding this. Thanks for your time :) Cheers, Ranga :) (0 Replies)
Discussion started by: rangarasan
0 Replies

3. Shell Programming and Scripting

Shell Scripting Problem - Invalid Back Reference

Here is the question... Create a new script, sub2, taking three parameters... 1.) the string to be replaced 2.) the string with which to replace it 3.) the name of the file in which to make the substitution ...that treats the string to be replaced as plain text instead of as a regular... (1 Reply)
Discussion started by: johnhisenburg
1 Replies

4. Programming

Passing Pointers by reference in C++ Problem

Hello All, I am having this issue...where I am actually having hard time understanding the problem: The code is as follows: #include<iostream.h> void fxn(char*** var) { int i =4; *var = (char**)malloc(i*sizeof(char*)); for(int j =0; j<4; j++) { *var = "name"; cout<<*var;... (6 Replies)
Discussion started by: mind@work
6 Replies

5. Shell Programming and Scripting

Help with rename header content based on reference file problem

I got long list of reference file >data_tmp_number_22 >data_tmp_number_12 >data_tmp_number_20 . . Input file: >sample_data_1 Math, 5, USA, tmp SDFEWRWERWERWRWER FSFDSFSDFSDGSDGSD >sample_data_2 Math, 15, UK, tmp FDSFSDFF >sample_data_3 Math, 50, USA, tmp ARQERREQR . . Desired... (7 Replies)
Discussion started by: perl_beginner
7 Replies

6. Shell Programming and Scripting

Replace character based on reference file problem asking

I got two files right now, input file (target file), reference file 1 (query file) reference file 1 (long list of data) KOLOPWMOPOPO ADASDASD ADSASDASDAD . . target file (one long liner content) ADASDASDTYUKOKOLOPWMOPOPOOPLUAADSASDASDADPOPOUYADADASDASD desired output file content ... (1 Reply)
Discussion started by: patrick87
1 Replies

7. Shell Programming and Scripting

BASH: File name part to list reference problem.

I've made a habit of including a four-letter "tail" on image file names I download from the Web, so I can both match them with IPTC Transmission References of my own making and rename them later using either a GUI renamer or a script I've written myself. Now I want to automate the process of... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

8. Shell Programming and Scripting

Array reference - bad substitution

I've created a series of arrays named as follows: row1 row2 row3 . . . row10 Each has 4 elements. I'm trying to echo the array elements out in a for loop. Here's what I have: for ((i=1;i<=10;i++)) do for ((j=1;j<=4;j++)) do eval out=${row`echo $i`} echo -n $out (3 Replies)
Discussion started by: swankgd
3 Replies

9. Shell Programming and Scripting

Reference two dimensional array in Perl sub

I am trying to reference a two dimensional array in a subroutine and can't seem to figure this one out in Perl. Does anybody know? Please enlighten me. #!/usr/bin/perl -w use constant DIM => 4; sub Shift_elements_right{ my (@Input, @Output) = @_; for ($i = 0 ; $i <= DIM ;... (5 Replies)
Discussion started by: photon
5 Replies
Login or Register to Ask a Question