luke123 has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to compare elements of 2 diffrent elements but it does not seem to be working.

First I read 2 files and put them in 2 different arrays. Then i want to compare elements of 2 arrays This is my code for comparing array elements:

if ($array2[2] eq $array1[2] ) { print $array2[2]; print "\n"; }
both of the elements of the 2 arrays are identical when I print them but the if statement evaluates to false anyaways? What is wrong with this statement?

Edit kudra, 2001-12-23 Added code tags

Replies are listed 'Best First'.
Re: comparing elements of two arrays
by mrbbking (Hermit) on Dec 24, 2001 at 00:47 UTC
    Your code works for me:
    @array1[2] = @array2[2] = 'hi'; if ($array2[2] eq $array1[2] ) { print $array2[2]; print "\n"; } # prints this: hi
    I suspect I'm missing something, though.

    Could you post your arrays, too? I think a little more detail is needed.

    Remember that 'eq' is for strings, and '==' is for numbers.

      mrbbking is right, we need more info from you. After seeing the solution for array from file and compare, i wonder if you need to chomp at least one of your arrays first.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      F--F--F--F--F--F--F--F--
      (the triplet paradiddle)
      
      My arrays are strings. Each elements is one line from a file. This is how I read the data and insert it into 2 arrays:
      $infile1 = "sept13.csv"; $infile2 = "sept14.csv"; open (infile1); @array1=<infile1>; close(infile1); open (infile2); @array2=<infile2>; close(infile2);
      The format of file(.csv) is as follows. Last Name, First Name, WebCT ID, Password, Courses Calvert,Aaron,aacalvert,0120015,lib101 Accardo,Andrea,aaccardo,0110659,biol361h:cost280h:lib100
        Slurping the whole file in like that results in a one-element array. So there is nothing at subscript 2 to compare.

        You'll need to split the lines of each file into their component parts before you can compare (what appears to be) usernames.

        ...does that make sense?

Re: comparing elements of two arrays
by vek (Prior) on Dec 24, 2001 at 04:23 UTC
Re: comparing elements of two arrays
by andye (Curate) on Dec 24, 2001 at 01:00 UTC
    Can't see much wrong with that. The only thing I can think of is: are the array elements numbers, or are they strings?

    If they're numbers, then of course it should be if ($array2[2] == $array1[2] ). If they're strings then you're doing it right.

    andy.

Re: comparing elements of two arrays
by beernuts (Pilgrim) on Dec 27, 2001 at 21:36 UTC
    I've always found it useful to print out my values surrounded by square braces when faced with similar problems. In any case, it'll definitely tell you if you need a chomp or not (as was suggested earlier). It also has the added side effect of showing you both values as seen by the comparison in the 'if'. Maybe something like this:
    if ($array2[2] eq $array1[2]){ print "$array[2]\n"; } else { print "\n\n[$array2[2]] [$array1[2]]\n\n"; }