Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How best to correlate the variables in two arrays?

Posted on 2015-01-06
6
Medium Priority
?
93 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
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…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

636 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