in reply to if else statement check an array

If you don't want to print PASS/FAIL on every iteration, you will need to remember the current overall status of PASS or FAIL, and only print it at the end. You could for example count the PASSes and FAILs, and if there are more than zero FAILs, output an overall FAIL. Also consider Test::Simple, to have ok(), fail() and a framework that checks the output of such scripts.

Replies are listed 'Best First'.
Re^2: if else statement check an array
by Marshall (Canon) on Jul 28, 2009 at 02:03 UTC
    Correct, easy to "remember this per iteration" is with scalar value of grep{}!

    Code below shows one line creation of a hash with values of array to be compared against. Then use of scalar value of grep which is # of matches which is used in logical sense, not numeric.

    Perl grep() is most often used in a "filter" sense, but it does have a scalar value.

    #!/usr/bin/perl -w use strict; my @test_a = qw (a b c d e); my @test_b = qw (a b c d e); my @test_c = qw (a b c d f); my %standard = map{$_ => 1}@test_a; if (grep{!$standard{$_}}@test_b) { print "FAIL test_b .. not all b in a\n"; } else { print "PASS test_b .. all b in a\n"; } if (grep{!$standard{$_}}@test_c) { print "FAIL test_c .. not all c in a\n"; } else { print "PASS test_c .. all c in a\n"; } __END__ PRINTS: PASS test_b .. all b in a FAIL test_c .. not all c in a