# Array help (variable substitution).

**URL:** <https://community.unix.com/t/array-help-variable-substitution/170452>\
**Category:** Shell Programming and Scripting\
**Created:** [April 11, 2007, 3:25pm UTC](https://community.unix.com/t/array-help-variable-substitution/170452 "2007-04-11T15:25:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dsimpg1](https://community.unix.com/letter_avatar/dsimpg1/32/6_449c533f586939b6e5f246632a46421f.png) [@dsimpg1](https://community.unix.com/u/dsimpg1)\
**Post date:** [April 11, 2007, 3:25pm UTC](https://community.unix.com/t/array-help-variable-substitution/170452/1 "2007-04-11T15:25:40Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [April 11, 2007, 3:50pm UTC](https://community.unix.com/t/array-help-variable-substitution/170452/2 "2007-04-11T15:50:39Z")

</div>

> [@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?

```plaintext
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

```

---

<div class="post-metadata">

**Author:** ![dsimpg1](https://community.unix.com/letter_avatar/dsimpg1/32/6_449c533f586939b6e5f246632a46421f.png) [@dsimpg1](https://community.unix.com/u/dsimpg1)\
**Post date:** [April 11, 2007, 4:04pm UTC](https://community.unix.com/t/array-help-variable-substitution/170452/3 "2007-04-11T16:04:46Z")

</div>

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:

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

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [April 11, 2007, 4:38pm UTC](https://community.unix.com/t/array-help-variable-substitution/170452/4 "2007-04-11T16:38:33Z")

</div>

> [@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:
> 
> ```plaintext
> 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...

```plaintext
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

```
