Solved

How best to correlate the variables in two arrays?

Posted on 2015-01-06
6
81 Views
Last Modified: 2015-01-07
I want to correlate the variables in two arrays.  What's the simplest way of doing this?  I presume using index?  I'd also like to confirm that they both contain the same quantity of elements.

E.g. for below 10.10.12.16 correlates to 04 etc.

STORAGE=(10.10.12.16 10.10.12.20 10.10.12.22)
LVM=(04 07 08)

for i in ${STORAGE[@]}
do
echo $i >> file.txt
echo CORRESPONDING_LVM_VALUE >> file.txt
done
0
Comment
Question by:mickt
  • 3
  • 3
6 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40533355
Yes, use an index:

#!/bin/bash
STORAGE=(10.10.12.16 10.10.12.20 10.10.12.22)
LVM=(04 07 08)

if [[ ${#STORAGE[@]} -ne ${#LVM[@]} ]]; then
 echo "Number of elements doesn't match"
 exit
fi

n=0
while [[ n -lt ${#STORAGE[@]} ]]
 do
  echo ${STORAGE[n]} >> file.txt
  echo ${LVM[n]} >> file.txt
  ((n++))
 done
0
 

Author Comment

by:mickt
ID: 40533384
Cheers

And the 2nd part of question similar to the following:

if [ ${#STORAGE[@]} != ${#LVM[@]} ]
  then
    echo "STORAGE and LVM arrays do not contain the same quantity of elements.  Exiting."
    exit 1

fi
0
 

Author Comment

by:mickt
ID: 40533390
Sorry, missed your first section :).  cheers again.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40533410
Why grade B? Did you find my solution incomplete?
0
 

Author Comment

by:mickt
ID: 40533414
Not at all, very good solution.  Wasn't paying attention.  Can I change it?
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40533434
Click "Request Attention" and ask the mods to reopen the question.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now