ronreiter / interactive-tutorials

Interactive Tutorials
Apache License 2.0
4.04k stars 2.56k forks source link

Array comparison #673

Open ellisju37073 opened 2 years ago

ellisju37073 commented 2 years ago

Hello

Just wondering why the code for below shows 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[@]}