2

Similar to this >> In bash, how can I check if a string begins with some value?, but not duplicated.

I have two arrays, and for each string inside the first one, I wanna check whether the strings in the second one start with the string from the first one or not.

array1=("test1","test2","test3");
array2=("test1 etc","test1 nanana","test2 zzz","test3 abracadabra");

for i in "${!array1[@]}"; do
  for j in "${!array2[@]}"; do 
    if [[ "${array1[i]}" == "${array2[j]}*" ]]; then  
      echo "array1[$i] and arry2[$j] initial matches!";
      fi; 
    done; 
done

I tried many conditions inside if, such as:

if [[ "${array1[i]}" == "${array2[j]*}" ]]
if [[ "${array1[i]}" == "${array2[j]}*" ]]
if [[ "${array1[i]}" = "${array2[j]*}" ]]
if [[ "${array1[i]}" = "${array2[j]}*" ]]

Also without quotes, braces, and so on, all with no success.

Community
  • 1
  • 1
gcpdev
  • 442
  • 6
  • 20

1 Answers1

3

There are some errors in your code, first of all, array declaration in bash: if you don't put spaces you have only one element. Remember to always print the variables before trying anything else on them. From the bash docs:

ARRAY=(value1 value2 ... valueN)

Each value is then in the form of [indexnumber=]string. The index number is optional. If it is supplied, that index is assigned to it; otherwise the index of the element assigned is the number of the last index that was assigned, plus one. This format is accepted by declare as well. If no index numbers are supplied, indexing starts at zero.

To cycle array elements:

for element in "${array[@]}"
do
    echo "$element"
done

Here is a code snippet:

array1=(test1 test2 test3);
array2=(test1 etc "test1 nanana" test2zzz test3 abracadabra);
for word1 in "${array1[@]}"; do
    for word2 in "${array2[@]}"; do 
        echo "w1=$word1, w2=$word2"
        if [[ ${word2} == ${word1}* ]]; then   
            echo "$word1 and $word2 initial matches!";
        fi;                   
    done;                         
done 

After the comment by the OP I realised that He was trying to use indices, to do that you have to use the "$" also for the indices "i" and "j". Here is a working solution:

for i in "${!array1[@]}"; do
    for j in "${!array2[@]}"; do    
        echo "${array1[$i]} ${array2[$j]}"
        if [[ ${array2[$j]} == ${array1[$i]}* ]]; then
            echo "$array1[$i] and $array2[$j] initial matches!";
        fi; 
    done; 
done
terence hill
  • 3,354
  • 18
  • 31
  • My code is longer; I am using "readarray -t array1" to set the arrays with command outputs, so I declared the arrays in wrong way but only here in the question - my fault. Also, I used "!" in the front of the array because I wanted "i" and "j" to be the indexes. But your solution did work good as well. Thanks. – gcpdev Feb 06 '17 at 10:58
  • 1
    Yeah, I didn't realised that you are trying to use the indices, while I think that my first solution is better with bash I update the answer to include the one you were trying to implement. – terence hill Feb 06 '17 at 11:29
  • perfect, just got another mistake in my code. Thank you very much. – gcpdev Feb 06 '17 at 11:55