Re: Checking a list for a value



David Gilden wrote:
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.

Thanks for any insight into this problem,

Your logic says to exit the loop on the first array element that DOES NOT
contain the string in $valToTest. You probably want:

my $testOk = -1;

foreach my $cat ( @myGoodList ) {
if ( $cat =~ /$valToTest/ ) {
$testOk = 1;
last;
}
}


However, if you don't mind looping through ALL the elements of the array you
could do it like this:

my $testOk = grep( /$valToTest/, @myGoodList ) ? 1 : -1;

Although the foreach loop is more efficient as it exits the loop when a match
is found.


If you are looking for an exact match then you should use 'eq' instead of the
match operator:

if ( $cat eq $valToTest ) {



John
--
use Perl;
program
fulfillment
.



Relevant Pages

  • Re: Looking for a way to convert dec to bin
    ... It's set in stone now for vb but yes, many languages won't work. ... designed languages the loop var is out of scope anyway. ... This loop will probably never exit because b is always 255 or less. ... Generally if I need the index then I won't use foreach. ...
    (microsoft.public.vb.general.discussion)
  • Re: Infinite Loops and Explicit Exits
    ... > CS> One of these proposals relaxes the current restriction that an EXIT ... > termination of the loop is not visible at that point. ... > terminating condition is visible in that context. ... > You now want to allow this remote procedure, ...
    (comp.lang.cobol)
  • Re: Count Lines in (Huge) Text Files
    ... A few years ago, I was doing some high-throughput disk stuff and my recollection is that I found the same thing you did: larger buffers only helped up to about 8K or so, and past that any improvement was minimal. ... me that with appropriate settings for its buffer, it should perform better, since it ought to be optimized for line-based i/o. ... Assuming what's hurting you in the explicit forloop is the retrieval of the data and not the counter increment, the above should perform basically as well as a plain foreach() loop. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Houston-related rants was Re: forced merges/inside lane merges/AASHTO "tapered merges"
    ... >> Are you sure about the North Loop and I-45? ... >> in particular the westbound exit for Fondren/Gessner. ... How could HCTRA put an exit ramp for eastbound ... and the West Houston Center Blvd exit still shows Old Westheimer ...
    (misc.transport.road)
  • Re: Strategies with SPARK which does not support exceptions
    ... The only way of interrupting a code sequence in SPARK is the exit ... pass' loop with an unconditional exit at the end. ...    Some complicated code that might set OK; ...
    (comp.lang.ada)