David Gilden am Samstag, 11. Februar 2006 21.54:
> I would like to loop through 'all' acceptable values in an array (my white
> list) then assign a pass or fail value.
>
> In the following code does not accomplish this, it needs to go through the
> entire list before assigning a value.
>
>
> #!/usr/bin/perl
>
> $valToTest = "ac";
>
> @myGoodList = ("10_rater","36_600","ac","cr914","ec12","fairwind");
> $testOk = -1;
>
> foreach my $cat (@myGoodList){
> if ($cat =~ /$valToTest/) {
> $testOk = 1;
> next;
> } else {
> $testOk = -1;
> # value not in list
> last;
> }
> }
>
> print "$testOk\n";
>
>
> I am going to guess that the foreach is not the right way to address this
> problem.
I think your quess is ok - IF you test for equalty, not matching.
Just use a hash; then you don't have to inspect every value, but can just test
for existence. Something like
#!/usr/bin/perl
use strict; # don't forget
use warnings; # those two
my $testval='ac'; # no need for double quotes
my %good=map {$_=>1}
qw/10_rater 36_600 ac cr914 ec12 fairwind/;
my $ok=exists $good{$testval} ? 1 : 0;
# or simply: my $ok=exists $good{$testval}
print 'Test result: ', $ok;
hth,
joe
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>