Array reference problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array reference problem
# 1  
Old 07-19-2010
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
# 2  
Old 07-19-2010
Code:
for d in $temp
do
    # your code here
done

Try that
# 3  
Old 07-19-2010
If variable temp contains the name of your array, I think you want to expand it like this:
Code:
for d in "${!temp[@]}"

Each element of the array named by temp will be quoted.
# 4  
Old 07-19-2010
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.
# 5  
Old 07-19-2010
Error

@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....
# 6  
Old 07-19-2010
can you tell us the output of below comands.
Code:
echo $MYHOST
echo $A_MYHOST

# 7  
Old 07-19-2010
$MYHOST
tragedy

$A_MYHOST

no value is displayed
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