Mr. John Kent wrote:
Encountering some unexpected behavior illustrated in the code
snippet below.

In the first foreach loop Seems like when I check for a match
between gid306 and the contents of the the ACTIVES array I get an
erroneous hit.

<snip>

Is this a bug in Perl or in my understanding?

You missed that when using grep(), $_ is set locally to each LIST element, i.e. in that context $_ does not represent the element in the @LOGGED array.

my(@ACTIVE) = qw {gid240 gid278 gid301};
my(@LOGGED) = qw {gid306 gid240 gid278 gid301};

# This doesn't work,  finds a match for every item in
# LOGGED, seems to be matching on "gid" but ignoring the number
foreach (@LOGGED){
    unless (grep /$_/,@ACTIVE){

Consequently, that is always true.

To get rid of the confusion, you can make use of a named variable in
the foreach loop:

    foreach my $logged (@LOGGED) {
        unless (grep { $logged eq $_ } @ACTIVE) {
            print "No Match for $logged\n";
        } else {
            print "found $logged in ACTIVES\n";
        }
    }

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to