Re: if else statement check an array
by Corion (Patriarch) on Jul 27, 2009 at 08:48 UTC
|
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.
| [reply] [d/l] [select] |
|
|
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
| [reply] [d/l] |
Re: if else statement check an array
by rovf (Priest) on Jul 27, 2009 at 10:16 UTC
|
While all the answers I've seen to your post are well done and right to the point, I wonder whether they really so helpful for you, because they seem to be missing what I think might be your main problem:
From the way you ask your question, I assume that you are not a programmer, i.e. have never taken an introductory class in programming. I conclude this because your question is not so much Perl-specific, but an elementary problem in program in general: How can I find out whether or not a predicate holds for every member in a set of data.
If my guess is correct (and I know that it happens all the time that someone who has never really learned to program, is suddenly given one day the task of programming something), then I can only recommend that you first spend one or two weeks just to learn the principles of programming before trying your skills on a new project. There are a lot of introductory books around (although I don't know one which is teaching Perl as the first programming language - but maybe some fellow monks can help out here). Actually I did find an introductory book online, though it is for Python, not for Perl: http://openbookproject.net//thinkCSpy/. In case you don't find anything suitable for Perl, you might also try this one first; once you've learned a few principles of programming, it is not too difficult to switch to a different language.
--
Ronald Fischer <ynnor@mm.st>
| [reply] [d/l] |
Re: if else statement check an array
by psini (Deacon) on Jul 27, 2009 at 08:52 UTC
|
If I understand correctly, what you want to get is a result of FAIL if any of the test in the loop fails.
So, you can set to a true value (1) a variable before the loop and reset to false (0) whenever a single test fails. At the end of the loop the var has the value you expected.
my $result = 1;
foreach my $issued_CM_commands (@issued_commands){
my $showconfig;
$result = 0 if index("@showconfig", "$issued_CM_commands") == 0
}
print "\n--",$result ? 'PASS' : 'FAIL',"\n";
(Untested)
Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."
| [reply] [d/l] |
Re: if else statement check an array
by cdarke (Prior) on Jul 27, 2009 at 09:20 UTC
|
| [reply] |
|
|
that prints a result for each line. I want a result for the checking of the whole array.
| [reply] |
|
|
The principle is the same!
| [reply] |
Re: if else statement check an array
by mzedeler (Pilgrim) on Jul 27, 2009 at 09:32 UTC
|
It looks like List::MoreUtils and grep are the right tools to solve your problem.
By the way, please indent your code. It's not just to make it look nicer - indenting will help you find many problems.
| [reply] |
Re: if else statement check an array
by GrandFather (Saint) on Jul 27, 2009 at 17:20 UTC
|
Until good indentation becomes second nature, get a copy of Perl Tidy and use it, especially before you post code here. The structure of well indented code is much easier to understand and edit correctly.
In a subtle fashion you have nested search loops. That is always a red flag, although it is sometimes unavoidable. In this case the creation of the match string for index is executed each time through your loop - that is the subtle nested 'loop'. The general fix to such nested loops is to use a hash lookup if you can in place of one of them. The following implements that in the contxt of your problem:
use strict;
use warnings;
my @issued_commands = qw(that or the other);
my @showconfig = qw(this that and the other);
my %hits = map {$_ => 0} @showconfig;
my @hit;
my @extra;
for my $issued_CM_command (@issued_commands) {
if (exists $hits{$issued_CM_command}) {
++$hits{$issued_CM_command};
push @hit, $issued_CM_command;
} else {
push @extra, $issued_CM_command;
}
}
my @missed = grep {! $hits{$_}} keys %hits;
print "No entry in \@issued_commands for: @missed\n" if @missed;
print "No entry in \@showconfig for: @extra\n" if @extra;
Prints:
No entry in @issued_commands for: and this
No entry in @showconfig for: or
Note that there is probably a lot of magic going on in there that is a little beyond where you are at with Perl at present. That is by intent! Please read the perldoc's for any Perl functions you aren't familiar with. If you still can't come to grips with it, please ask!
True laziness is hard work
| [reply] [d/l] [select] |
Re: if else statement check an array
by Selvakumar (Scribe) on Jul 27, 2009 at 10:52 UTC
|
Two things you can try to get the result. one with comparison of array and another grep method.
Also you want to check all elements presents in another array or if anything fails we can print fail or for single pass we can print pass.
| [reply] |
Re: if else statement check an array
by bichonfrise74 (Vicar) on Jul 27, 2009 at 22:13 UTC
|
Based on how I understand your question, I would do something like this. #!/usr/bin/perl
use strict;
my @issued_commands = qw( ls rm df );
my @showconfig = qw( df );
my $check = @issued_commands;
foreach my $issued_CM_commands (@issued_commands){
$check = (index("@showconfig", "$issued_CM_commands") > 0)
? $check : ++$check;
}
$check == @issued_commands
? print "Everything was in the array.\n"
: print "Everything was not in the array.\n";
| [reply] [d/l] |