Array help (variable substitution).


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Array help (variable substitution).
# 1  
Old 04-11-2007
Array help (variable substitution).

I am using an array (clmlist01). I have 61 of these and have 4 or more references to each one in a block of code that I do not want to have to hardcode. With that being said, I am creating a varible and going through a for loop to create the actually name of each array. The arrays would end up being cmlist01, cmlist02, cmlist03, etc. I don't have any problem creating the variables, but I seem to be doing something wrong in the syntax because if I use ${#cmlist01[@]} it gives me 11, but if I try to resolve the variable substituion name (listnbr), it resoves to cmlist01, but the ${#listnbr[@]} always returns 1. Is it possible to do what I am trying, or do you have to always hardcode the actual array name (instead of resoving it on the fly)? Help with the syntax please!
# 2  
Old 04-11-2007
Quote:
Originally Posted by dsimpg1
I am using an array (clmlist01). I have 61 of these and have 4 or more references to each one in a block of code that I do not want to have to hardcode. With that being said, I am creating a varible and going through a for loop to create the actually name of each array. The arrays would end up being cmlist01, cmlist02, cmlist03, etc. I don't have any problem creating the variables, but I seem to be doing something wrong in the syntax because if I use ${#cmlist01[@]} it gives me 11, but if I try to resolve the variable substituion name (listnbr), it resoves to cmlist01, but the ${#listnbr[@]} always returns 1. Is it possible to do what I am trying, or do you have to always hardcode the actual array name (instead of resoving it on the fly)? Help with the syntax please!

Are you looking for something like this?

Code:
q1=( q w e r t y u i o p )
q2=( a s d f g h j k l )
q3=( z x c v b n m )

for var in q1 q2 q3
do
  eval "printf '%d\n' \"\${#${var}[@]}\""
done


# 3  
Old 04-11-2007
No, I'm trying to do this:

cmlist01 has a b c d e
cmlist02 has f g h i
cmlist03 has j k l

For the total in cmlist01, I should see 5 items.
For the total in cmlist02, I should see 4 items.
For the total in cmlist03, I should see 3 items.

As a result, I could do:

print "There are ${#cmlist01[@]} items in $listnbr:"
which prints "There are 5 itmes in cmlist01."

However, I want to do all of this in a for loop and have it derive the cmlistnn from the variable $listnbr (which is does just fine as evidenced in the output from the print statement). However, if I try to get the count (number of items in the array) using listnbr (instead of hardcoding cmlistnn in each statement), all I ever get is 1.

If I set the variable listnbr=cmlist01, I should see 5 items, but am only seeing 1 when I do:

print "There are ${#listnbr[@]} items in $listnbr:"
which prints "There are 1 itmes in cmlist01."

The main part of the code that I need this for is:

Code:
while [ $i -lt ${#cmlist03[@]} ]
     do
          ID=${cmlist03[$i]}
          echo "Processing $i with ${cmlist03[$i]}"

Maybe you just can't do what I'm trying to do...

Last edited by dsimpg1; 04-11-2007 at 05:20 PM..
# 4  
Old 04-11-2007
Quote:
Originally Posted by dsimpg1
No, I'm trying to do this:

cmlist01 has a b c d e
cmlist02 has f g h i
cmlist03 has j k l

For the total in cmlist01, I should see 5 items.
For the total in cmlist02, I should see 4 items.
For the total in cmlist03, I should see 3 items.

As a result, I could do:

print "There are ${#cmlist01[@]} items in $listnbr:"
which prints "There are 5 itmes in cmlist01."

However, I want to do all of this in a for loop and have it derive the cmlistnn from the variable $listnbr (which is does just fine as evidenced in the output from the print statement). However, if I try to get the count (number of items in the array) using listnbr (instead of hardcoding cmlistnn in each statement), all I ever get is 1.

If I set the variable listnbr=cmlist01, I should see 5 items, but am only seeing 1 when I do:

print "There are ${#listnbr[@]} items in $listnbr:"
which prints "There are 1 itmes in cmlist01."

The main part of the code that I need this for is:

Code:
while [ $i -lt ${#cmlist03[@]} ]
     do
          ID=${cmlist03[$i]}
          echo "Processing $i with ${cmlist03[$i]}"

Maybe you just can't do what I'm trying to do...

Code:
cmlist01=( a b c d e)
cmlist02=( f g h i )
cmlist03=( j k l )

for listnbr in cmlist01 cmlist02 cmlist03
do
  eval "printf 'There are %d items in %s:\n' \"\${#${listnbr}[@]}\" \"$listnbr\""
  eval "printf '\t%s\n' \"\${$listnbr[@]}\""

done

This User Gave Thanks to cfajohnson For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need help with array substitution

Hello all, I have following piece of code which is working fine if executed standalone - date=$1 set -A max_month 0 31 28 31 30 31 30 31 31 30 31 30 31 eval $(echo $date|sed 's!\(....\)\(..\)\(..\)!year=\1;month=\2;day=\3!') (( year4=year%4 )) (( year100=year%100 )) (( year400=year%400 ))... (7 Replies)
Discussion started by: ektubbe
7 Replies

2. Shell Programming and Scripting

Variable substitution in array printing

Hi folks, A really dumb question as I've wasted far too long trying to get this to work.... (on RH bash) I have an array: m0='<hello>' m0='<there>' m0='<fred>' v0='<goodbye>' v0='<again>' v0='<john>' in my code I calculate the value of the variable to output and if I echo it, I... (2 Replies)
Discussion started by: say170
2 Replies

3. Shell Programming and Scripting

Variable substitution

Hi, I have to write a shell script in which I have to substitute a variable within a variable. For example, var1=aaa var2=file.$var1.txt The output should be, echo $var2 file.aaa.txt Can someone please help me in getting this. I tried using eval, but it didnt work. I might be using it... (2 Replies)
Discussion started by: grajp002
2 Replies

4. Shell Programming and Scripting

How to use variable with command substitution in variable

For example I have variable like below echo $OUTPUT /some/path/`uname -n` when I try to use the variable OUTPUT like below cd $OUTPUT or cd ${OUTPUT} I am getting bad substituion error message $ cd $OUTPUT ksh: cd: bad substitution $ cd ${OUTPUT} ksh: cd: bad substitution ... (1 Reply)
Discussion started by: rajukv
1 Replies

5. Shell Programming and Scripting

Substitution in a file dont work with an Array in filename

Hi. I´ve a script that should substitude the 8th line in a file called xxx.num6. The "xxx" is set by an array filled with this command: j=0 for Par in *.sys ; do Par=`echo $Par | sed 's/\(.*\).sys/\1/'` ; Par2="$Par" ; echo "${Par2}" j=$((j + 1)); done Now i try... (0 Replies)
Discussion started by: Lock3
0 Replies

6. Shell Programming and Scripting

variable substitution

file1.ksh #!/bin/ksh test5_create="I am a man" # test5 will be dynamic and the value will be passed from command line a=${1}_create echo $a # i need the output as "I am a man" ./file1.ksh test5 # i run the script like this any suggessions guys... (1 Reply)
Discussion started by: giri_luck
1 Replies

7. 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

8. Shell Programming and Scripting

Tricky array substitution in functions

Hello, Please tell me if there is a better way to get the number of elements from an array that is passed to a function. This is what works on Solaris 8 (ksh) but it looks odd: loop_array() { array_name=$2 b1='\${\#' b2='}' nr_elements=`eval echo... (6 Replies)
Discussion started by: majormark
6 Replies

9. Shell Programming and Scripting

Sed variable substitution when variable constructed of a directory path

Hello, i have another sed question.. I'm trying to do variable substition with sed and i'm running into a problem. my var1 is a string constructed like this: filename1 filerev1 filepath1 my var2 is another string constructed like this: filename2 filerev2 filepath2 when i do... (2 Replies)
Discussion started by: alrinno
2 Replies

10. UNIX for Advanced & Expert Users

Substitution in a variable

Hey All, I'm trying to clean up a variable using sed but It dosn't seem to work. I'm trying to find all the spaces and replace them with "\ " (a slash and a space). For Example "Hello World" should become "Hello\ World". But it does nothing. If I put it directly into the command line it works... (3 Replies)
Discussion started by: spragueg
3 Replies
Login or Register to Ask a Question