M
Max Moor
Hi All,
I'm fairly new to perl, and have already fixed this problem, but I
want to understand why it didn't work before.
This is the original, non-working code:
@o_list = ();
foreach my $o ( @{$stuff->{thingys}} ) {
# Exclude if no match
$match = 'n';
foreach $thingy (@test_thingys) {
if ($o->{thingy} == $thingy) {
$match = 'y';
last;
}
}
next if ($match != 'y');
push @o_list, $o;
}
Basically, its running through a list of "thingys", checking to see if
the item matches any of a list of test "thingys." Before checking the list,
I set $match to 'n'. If I find a match, I set it to 'y' and dump out of the
loop. At least, that was the intent. What really happened was that "next
if" line never ran, and all items got pushed onto "o_list." I did some
checking, and found that $match equaled 'n'. The line simply wasn't ever
running.
The working snippet...
@o_list = ();
foreach my $o ( @{$stuff->{thingys}} ) {
# Exclude if no match
$match = 0;
foreach $thingy (@test_thingys) {
if ($o->{thingy} == $thingy) {
$match = 1;
last;
}
}
next if (!match);
push @o_list, $o;
}
The only change was the "flag" values. Instead of 'y' and 'n', it's
now 0 and 1.
Can anyone tell me what about the 'n' and 'y' messed up the logic?
Thanks for your help,
Max
I'm fairly new to perl, and have already fixed this problem, but I
want to understand why it didn't work before.
This is the original, non-working code:
@o_list = ();
foreach my $o ( @{$stuff->{thingys}} ) {
# Exclude if no match
$match = 'n';
foreach $thingy (@test_thingys) {
if ($o->{thingy} == $thingy) {
$match = 'y';
last;
}
}
next if ($match != 'y');
push @o_list, $o;
}
Basically, its running through a list of "thingys", checking to see if
the item matches any of a list of test "thingys." Before checking the list,
I set $match to 'n'. If I find a match, I set it to 'y' and dump out of the
loop. At least, that was the intent. What really happened was that "next
if" line never ran, and all items got pushed onto "o_list." I did some
checking, and found that $match equaled 'n'. The line simply wasn't ever
running.
The working snippet...
@o_list = ();
foreach my $o ( @{$stuff->{thingys}} ) {
# Exclude if no match
$match = 0;
foreach $thingy (@test_thingys) {
if ($o->{thingy} == $thingy) {
$match = 1;
last;
}
}
next if (!match);
push @o_list, $o;
}
The only change was the "flag" values. Instead of 'y' and 'n', it's
now 0 and 1.
Can anyone tell me what about the 'n' and 'y' messed up the logic?
Thanks for your help,
Max