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 09-30-2010, 07:36 PM   #1
iBasher
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Rep: Reputation: 0
How do I compare two arrays in bash?


Hi All!

I am writing a script to get the multiples of 2 and 3, place them in an 2 arrays, and then show the common integers. So far everything works fine till the comparision. I don't know how to compare them. Here is the code:

Code:
#!/bin/bash
let num1="2"
let num2="3"
declare -a multiples1
declare -a multiples2
for (( i="0"; i<11; i="$i+1" ))
do
       let x${i}="$num1*$i"
done
multiples1=($x1 $x2 $x3 $x4 $x5 $x6 $x7 $x8 $x9 $x10)
echo ${multiples1[@]}
let i="0"
for (( i="0"; i<11; i="$i+1" ))
do
       let y${i}="$num2*$i"
done
multiples2=($y1 $y2 $y3 $y4 $y5 $y6 $y7 $y8 $y9 $y10)
echo ${multiples2[@]}
How can I do this? (explain)
 
Old 09-30-2010, 08:36 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,566

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
So I would probably like to back up a little and ask what the?
Sorry ... not trying to be rude but you are creating arrays by first creating strings and then creating arrays from that ... why?
Why not just put the values straight into the arrays?
Code:
num1=2

for (( i=0; i<=11; i++ ))
do
    multiples1+=($(( i * num1 )))
done
echo ${multiples1[@]}
echo ${#multiples1[@]}
The answer to the second part of your query about how to compare would be st be achieved with a loop in a loop
 
Old 09-30-2010, 08:43 PM   #3
iBasher
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Original Poster
Rep: Reputation: 0
sorry i didnt know how to put it into the array right away. I also dont know what you mean by:

Quote:
st be achieved with a loop in a loop
What loop (if,until,while,for)? What is st?
 
Old 09-30-2010, 09:08 PM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
loop inside a loop, with for loops.

Here's one way, with two `for` loops:
Code:
# loop in a loop (two for loops):
for xx in $(seq 1 ${#multiples2[@]}); do
  for yy in $(seq 1 ${#multiples1[@]}); do
    # see if the variable contains something, and if the two variables match:
    if [ "multiples1[$yy]" ] && [ "${multiples2[$xx]}" = "${multiples1[$yy]}" ]; then
      # if so, add them into an array to print at the end.
      out+=( ${multiples1[$yy]} )
    fi
  done
done

# Print the matching numbers:
echo "Common: ${out[@]}"

Last edited by GrapefruiTgirl; 09-30-2010 at 09:10 PM. Reason: remove unset - not needed.
 
Old 09-30-2010, 09:10 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
awk 'BEGIN{
  for(i=2;i<11;i+=2){
    for(j=3;j<11;j+=3){
        if (j==i){
          printf "i:%s, j:%s\n" ,i,j
        }
    }
  }
}'
 
Old 10-01-2010, 12:09 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,566

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
sorry ... mine was a typo, there should be no space between 'be' and 'st' so yes 'best' option is to use a loop of which by the way 'if' is not a loop.
Any of the others can work depending on your choice, but the for loop as shown by GrapefruiTgirl would be my choice.

I would offer some slight adjustments:
Code:
for xx in ${!multiples2[*]}; do
  for yy in ${!multiples1[*]}); do
    # see if the two variables match:
    if (( ${multiples2[xx]} == ${multiples1[yy]} )); then
      # if so, add them into an array to print at the end.
      out+=( ${multiples1[yy]} )
    fi
  done
done

# Print the matching numbers:
echo "Common: ${out[*]}"
Changes made for following reasons:

1. ${!array[*]} - * apparently runs quicker and ! will give you indices that have been assigned
2. [index] - variables used as indices do not require $ to be expanded
3. (( )) - doing number testing should be done using these parentheses

Last edited by grail; 10-01-2010 at 05:13 AM.
 
Old 10-02-2010, 12:12 PM   #7
iBasher
LQ Newbie
 
Registered: Sep 2010
Posts: 16

Original Poster
Rep: Reputation: 0
thanx
 
  


Reply


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
[SOLVED] bash and arrays disca Programming 3 07-27-2010 09:26 AM
Bash Arrays Simon256 Programming 2 02-17-2009 02:39 PM
How to compare two lists (arrays) in perl WindowBreaker Programming 13 04-24-2008 04:01 AM
[php] how to compare 2 arrays ? graziano1968 Programming 2 11-21-2006 08:30 AM

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

All times are GMT -5. The time now is 06:20 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration