Mohamed Nuzhy
Mohamed Nuzhy

Reputation: 105

If statement for two different arrays in bash

I have two arrays, which are arrAlpha[] and arrPT[]. Array arrAlpha contains the Alphabets and array arrPT[] contains some of the plain letters.following is the code that i wrote in bash shell script to compare elements of both arrays and to store the position elements of arrPT[] in arrAlpha[] to array arrT[]. But when i run i feel like something is wrong in if statement to print out the elements in arrT[]. can anyone help me please?

     #!/bin/bash

     arrAlpha=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)

     arrPT=(E K N R S W )
     lenPT=${#arrPT}
     declare -A arrT
     q=0
     for((i=0; i<lenPT; i++)) do
         for((j=i; j<26; j++)) do
             if [  ${arrPT[$i]}  =  ${arrAlpha[$j]}  ]; then
                 arrT[$q]=$j % 26;
                 ((++q));
             fi
          done
     done

     echo ${arrAlpha[@]}
     echo ${arrPT[@]}
     echo ${arrT[@]}

the expected output is to change elements arrPT to number 0 to 25.

    arrAlpha=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
    arrPT =(E K N R S W)
    arrT =(4 10 13 17 18 22)

Upvotes: 0

Views: 1013

Answers (2)

user6717040
user6717040

Reputation:

The main bug is with this line:

lenPT=${#arrPT}

which should be written like:

lenPT=${#arrPT[@]}

But, in fact, there is no need (even if it is not wrong to use it) for such variable, change this line:

for((i=0; i<lenPT; i++)) do

to:

for((i=0; i<${#arrPT[@]}; i++)) do

Some other issues:

  • There is no need to mod 26 the value of $j, it will never reach 26.
  • The array arrT seems to be an indexed array: -a no need for -A.
  • There is no need for variable q if each result will be in the same position as the index i
  • Please quote your expansions.
  • Use printf (more reliable) instead of echo.

The script with all the above done is:

#!/bin/bash

 arrAlpha=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
 arrPT=(E K N R S W )

 declare -a arrT

 for((i=0; i<${#arrPT[@]}; i++)) do
    for((j=i; j<${#arrAlpha[@]}; j++)) do
       [[  ${arrPT[i]}  ==  "${arrAlpha[j]}"  ]] && let arrT[i]=j
    done
 done

 printf '%s ' "${arrAlpha[@]}"  ; echo 
 printf '%3s ' "${arrPT[@]}"    ; echo 
 printf '%3s ' "${arrT[@]}" ; echo

Of course, the second loop could be removed by cutting that string at the character:

#!/bin/bash

Alpha=ABCDEFGHIJKLMNOPQRSTUVWXYZ
arrPT=(E K N R S W )

declare -a arrT

for((i=0; i<${#arrPT[@]}; i++)) do
    arrT[i]=${Alpha%"${arrPT[i]}"*}    # cut the string at the character.
    arrT[i]=${#arrT[i]}                # Use the len of the cut string.
done

printf '%s ' "$Alpha"           ; echo 
printf '%3s ' "${arrPT[@]}" ; echo 
printf '%3s ' "${arrT[@]}"      ; echo

Upvotes: 0

pakistanprogrammerclub
pakistanprogrammerclub

Reputation: 827

Here's a fixed version of your script - there are some style changes that I prefer

arrAlpha=(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z)
arrPT=(E K N R S W )
# array length needs index *
lenPT=${#arrPT[*]}
# seems arrT can be simple indexed array
declare -a arrT
q=0
for((i=0; i<lenPT; ++i)); do
  for((j=0; j<26; ++j)); do
    if [[ ${arrPT[i]} == ${arrAlpha[j]} ]]; then
# arithmetic inside $(())
      arrT[q++]=$((j % 26))
    fi
  done
done
echo ${arrAlpha[@]}
echo ${arrPT[@]}
echo ${arrT[@]}

Upvotes: 1

Related Questions