set array name dynamically


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting set array name dynamically
# 1  
Old 02-19-2009
set array name dynamically

Hi,

I am trying to name a set of arrays dynamically named as @array_$i (The $i will mean that I obtain a set of arrays called:
@array_1
@array_2
@array_3 etc.

i tried various methods like

@array_$i
@{array_$i}
etc.

any ways to do this

Thanks
# 2  
Old 02-19-2009
Not sure what shell and OS you're using, but I'll assume bash considering your array notation.

The following works in bash on Linux and AIX:

Code:
$ array_1[0]=0
$ array_1[1]=1
$ array_1[2]=2
$ array_2[0]=3
$ array_2[1]=4
$ array_2[2]=5
$ for arrNum in `seq 1 2`
> do
> for arrDim in `seq 0 2`
> do
> echo "array_${arrNum}[${arrDim}] = $( eval echo \${array_${arrNum}[${arrDim}]} )"
> done
> done
array_1[0] = 0
array_1[1] = 1
array_1[2] = 2
array_2[0] = 3
array_2[1] = 4
array_2[2] = 5
$

# 3  
Old 02-19-2009
Sorry, i forgot to say that i am looking for perl.

Let me explain the problem once again. Need to set the variable name when assigning the data.

for example, in my program i am trying to put something like this

Code:
foreach(@i){
@array_$i = /something/;
}

intention is
@array_1=/something/;
@array_2=/something/;
etc
# 4  
Old 02-19-2009
Sorry, don't know what I was thinking. Bash doesn't even notate arrays starting with an '@'.

Anyhow, the easiest way in perl is to use hashes (associative arrays):

Code:
#!/usr/bin/perl

@i = qw(one two three);
$j = 1;

foreach (@i) {
  $hash{$_} = "\$hash{$_} = $j";
  $j++;
}

print "$hash{one}\n";
print "$hash{two}\n";
print "$hash{three}\n";

# 5  
Old 02-19-2009
Yep, use a hash. Dynamic variable names in perl is possible using soft references but you shouldn't do that as it can lead to very buggy code. Use a hash.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash question: working with an array of previously set variable strings

while i've used arrays to work with variables, i've never used them to loop through a set of strings and wanted to ask the community for some feedback or assistance. let me be specific. here's my code: # URL port Variables port2195=`nc -z $url2195 2195` port2196=`nc -z $url2196 2196`... (5 Replies)
Discussion started by: hungryd
5 Replies

2. Shell Programming and Scripting

How to get value from array and set those values as a variable

I am new to ksh scripting, specially array. How do i get values from an array and set the value as variable and pass those variables to the different functions?? someone taught me how to get input from a file with have columns i need to read, but now i doesnt know how to set those value to be a... (7 Replies)
Discussion started by: gavin_L
7 Replies

3. Shell Programming and Scripting

Finding indices in an array nearest to a set of values

I have an two arrays. One array BINDIST consists of fences. I have another array XOFFS. Eg BINDIST = 0 10 20 30 40 50 60 XOFFS = 2 3 4 23 25 28 55 58 I want to find to find the indices of values in XOFFS that are closest to each BINDIST. My idea is to do as follows I create array... (7 Replies)
Discussion started by: kristinu
7 Replies

4. UNIX for Dummies Questions & Answers

How to set server's ip address, router, network mask and set if it is an internal or external ip?

Hello, I need to write a program which sets server's ip address, router, network mask. Program also should set if it is an internal or external ip. Maybe someone can help me ? Any information from u is very useful :b: I stopped at .. :( #!/bin/sh A=`hostname -i` echo "server ip address is $A"... (4 Replies)
Discussion started by: zagaruika
4 Replies

5. Shell Programming and Scripting

How to create dynamically associative array in PHP?

Hi, I have an html page like this: <html> <body> <form action="test.php" method = "post"> Enter your name:<input name="search" type = "text" size ="40"> <br> Enter your age:<input name="age" type = "text" size ="20"> <input type = "submit" name="submit" value="search"> <input type =... (1 Reply)
Discussion started by: vanitham
1 Replies

6. Shell Programming and Scripting

Result set into an array

Hi, I have an issue with the result set. I wanted to run db2 query against db2 server in unix environment using perl script. I wanted to get the result set into an array. $db=<<DB_Name>> connect to $db get connection state this is my query = SELECT DISTINCT 'R' FROM... (0 Replies)
Discussion started by: solo123
0 Replies

7. Shell Programming and Scripting

Unable to set a data to array

Hi All, Iam trying to set the value to the array... Still its not happening Following is the code: #!/usr/bin/ksh filenames="x"; filenames="y"; echo $filenames; echo $filenames; O/P: x x Iam expecting (2 Replies)
Discussion started by: kiranlalka
2 Replies

8. Shell Programming and Scripting

dynamically setting an array

Hi Gurus, How do I dynamically set up an array. Below is my code if ] then set ecomm_task_limit = '${ecomm_srvr}' fi Here, I want to set values in the array "${ecomm_srvr}" into ecomm_task_limit upon each iteration. Finally I want to display all the values in the new array... (4 Replies)
Discussion started by: ragha81
4 Replies

9. UNIX for Dummies Questions & Answers

How can i read array elements dynamically in bash?

Hi friends how can we read array elements dynamically in bash shell? (1 Reply)
Discussion started by: haisubbu
1 Replies

10. Shell Programming and Scripting

How to set dynamically keys names in perl %hash

hello I have loop , in this loop im picking names , this names I want to be keys in %hash but I don't know how to set in every loop entertain different key in the %hash (1 Reply)
Discussion started by: umen
1 Replies
Login or Register to Ask a Question