LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do I compare two arrays in bash? (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-compare-two-arrays-in-bash-835500/)

iBasher 09-30-2010 06:36 PM

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)

grail 09-30-2010 07:36 PM

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

iBasher 09-30-2010 07:43 PM

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?

GrapefruiTgirl 09-30-2010 08:08 PM

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[@]}"


ghostdog74 09-30-2010 08:10 PM

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
        }
    }
  }
}'


grail 09-30-2010 11:09 PM

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

iBasher 10-02-2010 11:12 AM

thanx


All times are GMT -5. The time now is 02:28 AM.