0

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)
2
  • What do you mean by "feel like something wrong" ? Did the output is not as you expected? Commented Aug 21, 2016 at 11:35
  • what i mean by feel like something is, i'm not sure about if statement that i wrote is correct or not. can u help me please? Commented Aug 21, 2016 at 12:11

2 Answers 2

1

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[@]}
Sign up to request clarification or add additional context in comments.

1 Comment

FYI, in [[ $foo == $bar ]], $bar is treated as a glob expansion unless quoted; if, for instance, it were *, it would match anything. If you don't want that behavior, consider quoting the right-hand side.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.