JEllis
JEllis

Reputation: 11

What is the reason for do in=false in the below script

I am doing bash and I was looking at a problem for array comparison and this was the solution below. I was wondering why they have the do in=false

#!/bin/bash
# enter your array comparison code here
# initialize arrays a b c
a=(3 5 8 10 6) 
b=(6 5 4 12) 
c=(14 7 5 7)
#comparison of first two arrays a and b
for x in "${a[@]}"; do 
  in=false 
  for y in "${b[@]}"; do 
    if [ $x = $y ];then 
      # assigning the matching results to new array z
      z[${#z[@]}]=$x
    fi
  done 
done
#comparison of third array c with new array z
for i in "${c[@]}"; do 
  in=false
  for k in "${z[@]}"; do
    if [ $i = $k ];then
      # assigning the matching results to new array j
      j[${#j[@]}]=$i
    fi
  done 
done 
# print content of array j
echo ${j[@]}

So should look like this correct? The first for loops do blank then the others analyze the arrays.

#!/bin/bash
a=(3 5 8 10 6) 
b=(6 5 4 12) 
c=(14 7 5 7)
#comparison of first two arrays a and b
for x in "${a[@]}"; do 

  for y in "${b[@]}"; do 
    if [ "$x" = "$y" ];then 
      # assigning the matching results to new array z
      z+=("$x")
    fi
  done 
done
#comparison of third array c with new array z
for i in "${c[@]}"; do 

  for k in "${z[@]}"; do
    if [ "$i" = "$k" ];then
      # assigning the matching results to new array j
     j+=("$i")
    fi
  done 
done 
# print content of array j
echo ${j[@]}

Upvotes: 0

Views: 78

Answers (0)

Related Questions