Solved

How best to correlate the variables in two arrays?

Posted on 2015-01-06
6
88 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 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
get remote physical servers and platforms 5 58
looking for unix program to search some keywords in side EAR WAR and JAR files 10 117
SSRS - Powershell 7 71
Unix Command (AIX) 3 49
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…

733 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