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

Hi Monks,

I am back again with probably a silly query.

Suppose I have a list and its elements contain exit codes as below:

list = {1,2,3,4,5,6,7,8,9}

I want to run a piece of code only if the exit code is anything but 3. This is what I have written but the code seems to be executing even if one of the element is 3. Not sure what I am doing wrong in my comparison. Any suggestions please?

if ($_ ne 3) { .... # do task };

I also tried: if ($_ !=~ 3) and if ($_ != 3) but to no avail.

Regards,

Jay
  • Comment on Do not run code even if one element has matched exit code value

Replies are listed 'Best First'.
Re: Do not run code even if one element has matched exit code value
by jdporter (Paladin) on Mar 12, 2015 at 15:16 UTC

    Uh... what does that list have to do with the logic of your code? What's the relationship? I don't see any.

    Also, maybe you should show a little more of your code. How is $_ getting set? That list definition isn't perl code, so...

Re: Do not run code even if one element has matched exit code value
by Marshall (Canon) on Mar 13, 2015 at 05:08 UTC
    A list in Perl is pretty much like an array although there are some fine differences between these two concepts. I think your nomenclature {} vs () and description may not be the best. The {} does not describe an array or a "list".

    An "exit code" says to me that this is single integer value from returned to the O/S, not a multi-valued thing or data structure - an "exit code" is an integer in Windows or Unix.

    I think you mean to check the return array value from a subroutine? This code checks that the main program only got one returned value and that that single value is "3".

    #!usr/bin/perl -w use strict; my @result; @result = X(); print "array returned from sub X is: ", @result,"\n"; if ((@result == 1) and ( grep {$_ == 3}@result )) { print "Sub X passes the test", @result,"\n"; } else { print "@result: for Sub X fails the test\n"; } print "\n"; @result = Y(); print "array returned from Sub Y is: ", @result,"\n"; if ((@result == 1) and ( grep {$_ == 3}@result)) { print "Sub Y @result passes the test\n"; } else { print "Sub Y @result fails the test\n"; } print "\n"; sub X { return (1,2,3,4,5,6,7,8,9); } sub Y { return (3); } __END__ array returned from sub X is: 123456789 1 2 3 4 5 6 7 8 9: for Sub X fails the test array returned from Sub Y is: 3 Sub Y 3 passes the test
    Update: well if the OP's intention is that should work for anything but 3, then the modifications should be obvious.
Re: Do not run code even if one element has matched exit code value
by chacham (Prior) on Mar 12, 2015 at 15:18 UTC

    I am back again with probably a silly query.

    Perhaps you meant to say simple query. There are no silly questions, just silly people. Or something like that. :)

    Anyway, it's best to ask what you don't know. Otherwise, how will you (and the rest of us) ever learn?

      Thanks. Below is the snippet:
      my @retCodes_values; open my $read_log_file, '<', '/tmp/app.log' or die $!; while(<$read_log_file>){ next unless /retCode\=/; my ($string, $exit_code) = split /retCode\=/,$_,2; push @retCodes_values, [ $string, $exit_code ]; } foreach(@retCodes_values) { if ($_!=~3) { read 2nd log file; read 3rd log file; } }

      ____app.log______

      15288 :: Mon Mar 2 13:23:32 2015::App.pm:status returned with retCode=1

      15288 :: Mon Mar 2 13:24:32 2015::App.pm:status returned with retCode=3

      15288 :: Mon Mar 2 13:25:32 2015::App.pm:status returned with retCode=4

      15288 :: Mon Mar 2 13:23:32 2015::App.pm:status returned with retCode=5

        The main issue is that you are creating an array of arrays, which you then need to dereference:
        use warnings; use strict; my @retCodes_values; while (<DATA>) { next unless /retCode\=/; chomp; my ( $string, $exit_code ) = split /retCode\=/, $_, 2; push @retCodes_values, [ $string, $exit_code ]; } for my $aref (@retCodes_values) { if ($aref->[1] != 3) { print "@{ $aref }\n"; } } __DATA__ 15288 :: Mon Mar 2 13:23:32 2015::App.pm:status returned with retCode= +1 15288 :: Mon Mar 2 13:24:32 2015::App.pm:status returned with retCode= +3 15288 :: Mon Mar 2 13:25:32 2015::App.pm:status returned with retCode= +4 15288 :: Mon Mar 2 13:23:32 2015::App.pm:status returned with retCode= +5

        Outputs:

        15288 :: Mon Mar 2 13:23:32 2015::App.pm:status returned with 1 15288 :: Mon Mar 2 13:25:32 2015::App.pm:status returned with 4 15288 :: Mon Mar 2 13:23:32 2015::App.pm:status returned with 5

        See also:

        Your snippet is not fully functional. Give a minute to clean it up: Never mind, toolic is way ahead of me.

        #!/usr/bin/perl use strict; my @retCodes_values; open my $read_log_file, '<', '/tmp/app.log' or die $!; while(<$read_log_file>){ next unless /retCode\=/; my ($string, $exit_code) = split /retCode\=/,$_,2; push @retCodes_values, [ $string, $exit_code ]; } foreach(@retCodes_values) { if ($_!=~3) { read 2nd log file; read 3rd log file; } } exit; __END__ D:\PerlMonks>test2.pl Bareword found where operator expected at D:\PerlMonks\test2.pl line 1 +9, near "2nd" (Missing operator before nd?) Bareword found where operator expected at D:\PerlMonks\test2.pl line 2 +0, near "3rd" (Missing operator before rd?) Not enough arguments for read at D:\PerlMonks\test2.pl line 19, near " +2nd " syntax error at D:\PerlMonks\test2.pl line 19, near "2nd " Not enough arguments for read at D:\PerlMonks\test2.pl line 20, near " +3rd " syntax error at D:\PerlMonks\test2.pl line 20, near "3rd " Execution of D:\PerlMonks\test2.pl aborted due to compilation errors.