Get Array Element Size [Duplicate]

I created an array of a sequence of numbers. How can I check the size of each element, and depending on the number of digits, each element has I want to add more numbers to it. Below I have some code that accomplishes this using one variable, but I want to do it with the array

#array I would like to use 
start=0
end=20
myarray=( $(seq $start  $end) )

#example of how I accomplish this using a variable. checks if the number of integers is between 1-5
and adds some numbers to the end. I would like to accomplish this but with an array.

number=2

local newnum=`expr length "$number"` #get lenght of the variable

if [ "$newnum" -eq "1" ];then 
    number="0000${number}" 

elif [ "$newnum" -eq "2" ];then
      number="000${number}"   

elif [ "$newnum" -eq "3" ];then
     number="00${number}"

elif [ "$newnum" -eq "4" ];then
    number="0${number}"

elif [ "$newnum" -eq "5" ]; then
    echo "you enter five numbers"
    #number remains the size 
else
    exit
fi #end if 







0

One way:

for val in "${myarray[@]}"
do
    len=${#val}
    echo $val , $len
    ....do action depending on $len .....
done
4
Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest