On Thu, 26 Jul 2001, Jennifer Pan wrote:

> I want to test if "AF00001" is in my list @mylist;
>
> I did:
>
> foreach $LIST (@mylist) {
>       if ($LIST = "AF00001")
>               $boolean = 1;
>       else
>               $boolean = 0;
> }
>
> is there a more elegant way to do it?

Sure is.  First of, your line with $LIST = "AF00001" won't work, because =
is for assignment, not testing for equality.  You want to use eq (use ==
for numerical values only.  You should also avoid using all caps for
variables -- use all caps for constants or filehandles.  This is a style
thing.

Here's how I would code this:

foreach (@mylist) {

        $boolean = ($_ eq 'AF00001') ? 1 : 0;
}

The conditional operator, ?:, tests an expression, and returns the first
value if the test is true, and the second if the test is false.

-- Brett

Brett W. McCoy
Software Engineer
Broadsoft, Inc.
240-364-5225
[EMAIL PROTECTED]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to