LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-28-2023, 10:38 AM   #1
Faki
Member
 
Registered: Oct 2021
Posts: 574

Rep: Reputation: Disabled
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

Last edited by Faki; 04-28-2023 at 10:40 AM.
 
Old 04-28-2023, 10:58 AM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,678

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
$i is each item i.e contents of each element of the array not the index.
 
Old 04-28-2023, 11:00 AM   #3
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-28-2023, 11:14 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,678

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
Either way.
 
Old 04-28-2023, 12:38 PM   #5
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
What is the way to print the index when using

Code:
for item in "${anciator[@]}"
 
Old 04-28-2023, 01:17 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,678

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
Code:
for i in "${!anciator[@]}"; do
  printf 'Current Index %s with value %s\n' "$i" "${anciator[i]}"
done
 
Old 04-28-2023, 01:50 PM   #7
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
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.

Last edited by Faki; 04-28-2023 at 02:06 PM.
 
Old 04-28-2023, 02:39 PM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
no, please do not use bash, it is not capable to do advanced handling of anything. It is nto meant to do that.
 
Old 04-28-2023, 02:39 PM   #9
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,136
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
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

Last edited by teckk; 04-28-2023 at 04:20 PM.
 
1 members found this post helpful.
Old 04-29-2023, 12:43 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,780

Rep: Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198Reputation: 1198
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.
 
Old 04-29-2023, 06:49 AM   #11
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,136
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
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
 
Old 04-29-2023, 01:00 PM   #12
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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.
 
Old 04-29-2023, 01:13 PM   #13
Faki
Member
 
Registered: Oct 2021
Posts: 574

Original Poster
Rep: Reputation: Disabled
How is that the output is just this?

Code:
One

Three
 
Old 04-29-2023, 09:18 PM   #14
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,136
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
#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
 
Old 04-30-2023, 01:56 AM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,792

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Quote:
Originally Posted by Faki View Post
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
 
  


Reply

Tags
bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
[SOLVED] reference bash array values, using a variable with a value of the array name gusthecat Programming 5 03-07-2012 03:41 PM
[bash] indirect array reference to array with values containing spaces Meson Linux - Software 9 06-04-2010 09:38 PM
Problem displaying Bash array values. shinobi59 Programming 3 01-17-2006 05:45 PM
How to return values into an array using awk Helene Programming 1 05-01-2004 10:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration