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

Hello PerlMonks I am looking to determine why an 'if' statement is failing. I am using it to compare elements of the 2 arrays. I have it working for the first elements of the arrays but not for the remainder even though when I print the offending elements they look the same. Any perls of wisdom?? .. code below...
#compare the commands in the arrays if (($commands_run[0] =~ /$commands[0]/) &&($commands_run[1] =~ /$commands[1]/) &&($commands_run[2] =~ /$commands[2]/) &&($commands_run[3] =~ /$commands[3]/) &&($commands_run[4] =~ /$commands[4]/)) { print "Test -----PASS\n"; } else { print "Test ---- FAIL\n"; }

Replies are listed 'Best First'.
Re: if statement comparing elements of arrays?
by Corion (Patriarch) on Jul 14, 2009 at 09:29 UTC

    How do you print them? If there is whitespace at the start or end, they will look similar but still won't run. Also, are you sure that @commands contains regular expressions that will match? Do something like the following:

    for my $i (0..$#$commands_run) { print "$i: '$commands_run[$i]' =~ /$commands[$i]/"; print $commands_run[$i] =~ /$commands[$i]/ ? ", Yes" : ", No"; print "\n"; };

    That should give you a good overview.

    Also, you might want to consider instead of having that long if statement using a loop similar to the one I showed for the actual testing. This will also help you to provide better diagnostic output if your test fails, as you can show which condition failed.

Re: if statement comparing elements of arrays?
by JavaFan (Canon) on Jul 14, 2009 at 09:46 UTC
    Are the elements of @commands supposed to be patterns? Or do you want exact matches (use eq)? Or perhaps check for a substring (use index)? Because if you want to do either of them, and one or more of the commands contain something that is special to the regexp engine (parens, *, +, etc), it isn't going to work.
Re: if statement comparing elements of arrays?
by ikegami (Patriarch) on Jul 14, 2009 at 15:00 UTC
    I bet that @commands does not contain regex patterns, but you treat its contents as such.
    =~ /$commands[0]/ # Matches pattern in $commands[0] =~ /\Q$commands[0]\E/ # Contains string in $commands[0] =~ /^\Q$commands[0]\E\z/ # Equals string in $commands[0] eq $commands[0] # Equals string in $commands[0]

    \Q..\E is a shortcut for calling quotemeta.

    And here's a solution that avoids redundancy:

    if ( grep { $commands_run[$_] ne $commands[$_] } 0..$#commands ) { print "Test ---- FAIL\n"; } else { print "Test -----PASS\n"; }
Re: if statement comparing elements of arrays?
by si_lence (Deacon) on Jul 14, 2009 at 09:32 UTC
    Your code works for me
    use strict; use warnings; #compare the commands in the arrays my @commands_run = qw(first second third fourth fifth); my @commands = qw(first second third fourth fifth); if (($commands_run[0] =~ /$commands[0]/) &&($commands_run[1] =~ /$commands[1]/) &&($commands_run[2] =~ /$commands[2]/) &&($commands_run[3] =~ /$commands[3]/) &&($commands_run[4] =~ /$commands[4]/)) { print "Test -----PASS\n"; } else { print "Test ---- FAIL\n"; }
    prints
    Test -----PASS
    So maybe your two arrays are not the same after all.
    Maybe print them with Data::Dumper to confirm?

    Cheers, si_lence

Re: if statement comparing elements of arrays?
by jeanluca (Deacon) on Jul 14, 2009 at 11:16 UTC
    it might help to inspect your arrays with Data::Dumper
    use Data::Dumper ; my @arr = qw(1 2 3 4 5) ; print Dumper(\@arr) ;