Greetings,
Hello,
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.
But as shown in the second foreach loop if I remove gid and just try to match the number 306 it correctly determines there is no match.
Is this a bug in Perl or in my understanding?
Thanks
John Kent
#!/usr/bin/perl
always put
use strict;
use warnings;
here. that will solve 99% of the problems for you by telling you what is off ;p
my($DEBUG) = 1;
my(@ACTIVE) = qw {gid240 gid278 gid301}; my(@LOGGED) = qw {gid306 gid240 gid278 gid301};
No need to surround those variable/array neame with ()
my $DEBUG = 1; my @ACTIVE = ...
Also it works but is confusing to use curly braces qith qw.
my @LOGGED = qw(gid306 gid240 gid278 gid301);
is better because there's no need to guess if you're trying to do an array of items or an array with one hashref in it.
# This doesn't work, finds a match for every item in # LOGGED, seems to be matching on "gid" but ignoring the number
Your $_ are going out of scope. the grep() $_ is an item in @ACTIVE and in that case will always be true.
Also the unless and else is a bit odd
Try nameing the variables:
foreach (@LOGGED){ unless (grep /$_/,@ACTIVE){ print "No Match for $__\n" if ($DEBUG == 1);
I think you meant $_ not $__ strict and warnings would have told you about that.
#do something here with what didn't match"; } else { print "found $_ in ACTIVES\n" if ($DEBUG == 1);
Again, no () are really necessary
Also you can shorten it to if $DEBUG; then set $DEBUG to 0 when you don't want that output.
} }
Here it is with all the changes, (necessary and cosmetic):
#!/usr/bin/perl
use strict; use warnings;
my $DEBUG = 1; my @ACTIVE = qw(gid240 gid278 gid301); my @LOGGED = qw(gid306 gid240 gid278 gid301);
for my $logd(@LOGGED) { if(grep { $logd eq $_ } @ACTIVE) { print "Found $logd in ACTIVES\n" if $DEBUG; } else { print "No match for $logd\n" if $DEBUG; } }
__END__
No match for gid306 Found gid240 in ACTIVES Found gid278 in ACTIVES Found gid301 in ACTIVES
HTH :) Lee.M - JupiterHost.Net
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>