×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

shell Associate Array not printing out all elements

shell Associate Array not printing out all elements

shell Associate Array not printing out all elements

(OP)
#!/opt/local/bin/bash

THRESHOLD=0
SUBJECT_HOURLY="'Hourly'"
SUBJECT_DAILY="'Daily'"

DAILY=`df -h | grep -i "/Volumes/Daily" | awk '{print $5}' | sed "s/%//"`
HOURLY=`df -h | grep -i "/Volumes/Hourly" | awk '{print $5}' | sed "s/%//"`

declare -A MyArray
MyArray=( [$HOURLY]=$SUBJECT_HOURLY [$DAILY]=$SUBJECT_DAILY )


for KEY in "${!MyArray[@]}"; do
echo "$KEY" "${MyArray[$KEY]}"
done

------------------
Expected result:
2 'Hourly'
2 'Daily'

Wrong result I always get is:
2 'Daily'

Why is my code not printing out the 1st key/value? It seems to skip the 1st key/value every time. Even if I swap the key pairs positions,
it just prints the 2nd key/value.

RE: shell Associate Array not printing out all elements

Hi

Quote (rigstars2)

Expected result:
2 'Hourly'
2 'Daily'
How could it do that ? I am not aware of any programming language where an array/associative array/hash/map can have more than one entries with identical index/key. If both HOURLY and DAILY have the same value 2, then the later added one will overwrite the previously added.

Feherke.
feherke.github.io

RE: shell Associate Array not printing out all elements

(OP)
feherke,

You are right on! The keys cannot be the same but the values can be. Just re-arranged MyArray and works great now! Appreciate the assist!

#!/bin/bash

SUBJECT_HOURLY="Disk 'Hourly'"
SUBJECT_DAILY="Disk 'Daily'"

DAILY_VALUE="2"
HOURLY_VALUE="2"

declare -A MyArray
MyArray=( [$SUBJECT_HOURLY]=$HOURLY_VALUE [$SUBJECT_DAILY]=$DAILY_VALUE )

for KEY in "${!MyArray[@]}"; do
echo "$KEY" "${MyArray[$KEY]}"
done

---------
Disk 'Hourly' 2
Disk 'Daily' 2

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close