in reply to Re: comparing array elements
in thread comparing array elements

the hash backfired! so i'm using a for loop. (little success so far) my comparison operator isn't working (both arrays contain 4- letter words) and i just need to determine which words are contained in the same position in each array.

thanks for helping!

for (my $i =0; $i < @forward_tets; $i++) { if ($forward_tets[$i] =~ $reverse_tets[$i]) { print "$forward_tets[$i] \t $reverse_tets[$i]\n" +; } }

Replies are listed 'Best First'.
Re: comparing array elements
by Abigail-II (Bishop) on Nov 21, 2003 at 09:51 UTC
    To compare two strings for equality, one uses eq, not =~ (although, if the words just contain letters, it would work too).

    Abigail

      yep, but this loop seems to be comparing every position in the first array to every position in the second, not one at a time! How can I do this?

        Why do you need two for-loops? Wouldn't one work?

        my @array1 = ('test', 'text', 'next'); my @array2 = ('text', 'test', 'next'); for (my $i=0; $i < scalar(@array1); $i++) { if ($array1[$i] eq $array2[$i]) { print "$array1[$i] matches\n"; } }
        How's that possible? You index both arrays with the same key.

        Abigail