LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Printing array values (https://www.linuxquestions.org/questions/linux-newbie-8/printing-array-values-4175724555/)

Faki 04-28-2023 10:38 AM

Printing array values
 
I am getting the following printed text. Why is bash complaining printing the array components ?

Code:

Anciator:
25uu failed numeric validation
-p Unrecognised Predicate
bash: 25uu: value too great for base (error token is "25uu")

Code:

 
if (( ${#anciator[@]} > 0 )); then
  printf '%s\n' "Anciator: ${anciator[@]}"
  for i in "${anciator[@]}"
  do
    printf '%s\n' "└── ${anciator[i]}"
  done
fi


michaelk 04-28-2023 10:58 AM

$i is each item i.e contents of each element of the array not the index.

Faki 04-28-2023 11:00 AM

What is the general way to print elements?

With

Code:

for i in "${anciator[@]}"
or via

Code:

for (( j=0; j<${length}; j++ ));
do
  printf "Current index %d with value %s\n" $j "${anciator[$j]}"
done


michaelk 04-28-2023 11:14 AM

Either way.

Faki 04-28-2023 12:38 PM

What is the way to print the index when using

Code:

for item in "${anciator[@]}"

michaelk 04-28-2023 01:17 PM

Code:

for i in "${!anciator[@]}"; do
  printf 'Current Index %s with value %s\n' "$i" "${anciator[i]}"
done


Faki 04-28-2023 01:50 PM

What would be the equivalent for a counter like so

Code:

for (( j = 0; j < ${#array[@]}; j++ ))
do
  printf "[%d] %s\n" $j "${anciator[$j]}"
done

I wonder whether using a counter as above could fail. The most secure way for printing seems to involve use of "${anciator[@]}" or "${!anciator[@]}", rather than the above.

pan64 04-28-2023 02:39 PM

no, please do not use bash, it is not capable to do advanced handling of anything. It is nto meant to do that.

teckk 04-28-2023 02:39 PM

Code:

A=(One Two Three Four)
B=(Cat Dog Cow Bird)

#Alternate items from 2 arrays
for i in "${!A[@]}"; do
    echo -e ""${A[i]}"\n"${B[$i]}""
done

echo -e ''$_{1..50}'\b─'

for ((j = 0; j < ${#A[@]}; j++)); do
    echo -e ""${A[j]}"\n"${B[$j]}""
done

Edit:
Sorry, I put an i in there instead of a j

MadeInGermany 04-29-2023 12:43 AM

Looping over the array values is most simple if you only want the values.

If you want the indexes and the values then you must loop over the indexes.

In bash the normal arrays start with index 0.
And normally are contiguous, so the following works.

Code:

arr=( a b c d )
for (( j=0; j < ${#arr[@]}; j++ ))
do
  printf "[%d] %s\n" $j "${arr[$j]}"
done

But in bash you can delete an array element. (Arrays are "sparse".)
Code:

unset arr[1]
Then the array is no longer contiguous, and only the following works correctly:
Code:

for j in ${!arr[@]}
do
  printf "[%d] %s\n" $j "${arr[$j]}"
done

This is because the ${!arr[@]} generates the existing keys.
And of course "${arr[@]}" generates the existing values.

teckk 04-29-2023 06:49 AM

Interesting. I don't think that I noticed that before.

Code:

A=(One Two Three Four)

for ((j = 0; j < ${#A[@]}; j++)); do
    echo -e "${A[$j]}"
done

One
Two
Three
Four

unset A[1]

for ((j = 0; j < ${#A[@]}; j++)); do
    echo -e "${A[$j]}"
done

One

Three

for i in "${!A[@]}"; do
    echo -e "${A[$i]}"
done

One
Three
Four


Faki 04-29-2023 01:00 PM

Quote:

Originally Posted by pan64 (Post 6427592)
no, please do not use bash, it is not capable to do advanced handling of anything. It is not meant to do that.

Have been thinking about your comment recently and plan to do some things in `perl`. Still for some of the things I currently have, an implementation in bash, even though crude, is good to have.

Faki 04-29-2023 01:13 PM

How is that the output is just this?

Code:

One

Three


teckk 04-29-2023 09:18 PM

#10 answers that.

Why don't you run some test yourself.

Code:

A=(One Two Three Four five Six Seven Eight)

for key in "${!A[@]}"; do
    echo "Key "$key" is Value "${A[$key]}""
done

unset A[3]

for key in "${!A[@]}"; do
    echo "Key "$key" is Value "${A[$key]}""
done



B=(One Two Three Four five Six Seven Eight)

for ((j = 0; j < ${#B[@]}; j++)); do
    echo "Key "$j" is Value "${B[$j]}""
done

unset B[3]

for ((j = 0; j < ${#B[@]}; j++)); do
    echo "Key "$j" is Value "${B[$j]}""
done


pan64 04-30-2023 01:56 AM

Quote:

Originally Posted by Faki (Post 6427787)
How is that the output is just this?

Code:

One

Three


Code:

A=(One Two Three Four)
unset A[1]

set -xv

for ((j = 0; j < ${#A[@]}; j++)); # ${#A[@]} is the number of elements, it is now 3
do   
    echo -e "${A[$j]:-missing}";
done
+ (( j = 0 ))                    # j=0 initialization
+ (( j < 3 ))                    # j < ${#A[@]}, do the cycle
+ echo -e One                    # print A[0]
One
+ (( j++ ))                      # increment j (j is now 1)
+ (( j < 3 ))                    # j < ${#A[@]}, do the cycle
+ echo -e 'missing'              # print A[1]
missing                          # We have no A[1], it was unset
+ (( j++ ))                      # increment j (j is now 2)
+ (( j < 3 ))                    # j < ${#A[@]}, do the cycle
+ echo -e Three                  # print A[2]
Three
+ (( j++ ))                      # increment j (j is now 3)
+ (( j < 3 ))                    # j < ${#A[@]} is false now, end of loop, exit
              # j = 3            # skipped



All times are GMT -5. The time now is 06:56 PM.