0

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 61 times. With that being said, I am creating a varible and going through a for loop to create the actual 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
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by thekashyap
0

I don't think it's possible though I'm not 100% sure.
Here is what I found in the advanced scripting link I refer to.

http://www.tldp.org/LDP/abs/html/parameter-substitution.html#PARAMSUBREF

Variable length / Substring removal
${#var}
String length (number of characters in $var). For an array, ${#array} is the length of the first element in the array.
Exceptions:
* ${#*} and ${#@} give the number of positional parameters.
* For an array, ${#array
[*]} and ${#array[@]} give the number of elements in the array.

So if you ask me to guess (why it prints 1) I'll say either it's printing the length of the first element, or it's printing the number of elements in the array.

Edited by pyTony: fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.