in reply to Break perl foreach loop

Maybe it would help if you added a print just after the loop, to confirm you have exited the loop, as follows:

} if ( $x =~ m/^Patient/ ) { print "matched here"; last WID; } } print "out of loop"; if ( $obj->has_members ) { dump_members($obj); } }

I wonder what output dump_members($obj) is producing.

update3 I suspect you are calling your subroutine more than once. Each call produces one or two tagged (e.g. <DATE></DATE>) blocks of data. Your loop within the subroutine is probably exiting as you expect (note that if you put the last before your print statement the print doesn't execute) but the caller then calls again with more data and your subroutine produces more output. You can test for this by adding a print "starting dump_afp" at the start of your subroutine.

update: If you post a dump of the object you are passing to the subroutine I can try running it and let you know what happens on my system.

update2: Here is the result of running your sub through B::Deparse. I see nothing obviously wrong.

$ perl -MO=Deparse test.pl BEGIN { $^W = 1; } sub dump_afp { my $obj = shift @_; my $struct = $obj->struct; my(@keys) = sort(grep((!/^_|^(?:Data|EscapeSequence|ControlCode|Le +ngth|CC|(?:Sub)?Type|FlagByte)$/), keys %$struct)); push @keys, 'Data' if exists $$struct{'Data'}; WID: foreach my $key (@keys) { next if ref $$struct{$key}; next unless length($x = $$struct{$key}); if ($obj->ENCODING and grep {$key eq $_;} $obj->ENCODED_FIELDS +) { $x = $obj->$key; $x = qq["$x"]; } elsif ($x =~ /[^\w\s]/) { $x = ''; } if ($key eq 'Data') { if ($x =~ /^"(\w|\d|\$)/) { $x =~ s/"|\(|\)//g; if ($x =~ m[^\d\d/\d\d/\d\d\s]) { my(@dateinfo) = split(/\s/, $x, 0); if ($dateinfo[0] =~ m[^\d\d/\d\d/\d\d]) { print '<DATE>'; print $dateinfo[0]; print "</DATE>\n"; print '<DESCRIPTION>'; print $dateinfo[1]; print "</DESCRIPTION>\n"; } } else { if ($x =~ m[^\d\d/\d\d/\d\d]) { print '<DATE>'; print $x; print "</DATE>\n"; } if (not $x =~ m[^\d\d/\d\d/\d\d] and $x != 'Patien +t') { if (not $x =~ /^((\d+)||(\d+,\d+))\.\d\d/) { print '<DESCRIPTION>'; print $x; print "</DESCRIPTION>\n"; } } } if ($x =~ /^((\d+)||(\d+,\d+))\.\d\d/) { print '<AMOUNT>'; print $x; print "</AMOUNT>\n"; } if ($x =~ /^((\$\d+)||(\$\d+,\d+))\.\d\d/) { print '<TOTAL>'; print $x; print "</TOTAL>\n"; } } } if ($x =~ /^Patient/) { last WID; } } if ($obj->has_members) { dump_members($obj); } } test.pl syntax OK

Replies are listed 'Best First'.
Re^2: Break perl foreach loop
by pvecchio (Initiate) on Mar 03, 2009 at 20:04 UTC
    Nice to back in the Perl community with so many helpful people...

    I tried the suggesting of printing "out of loop" and it provides a great clue. It prints "out of loop" all over the place, so it's looping through many, many times.

    eg.

    out of loop out of loop out of loop out of loop out of loop out of loo +p out of loop out of loop <TOTAL>$1,587.00</TOTAL> out of loop out of loop out of loop out of loop matched here out of lo +op out of loop out of loop out of loop out of loop out of loop out of + loop out of loop out of loop out of loop out of loop out of loop out + of loop out of loop out of loop <DATE>10/08/08</DATE>
    The dump_members subroutine at the end is the initial routine that parses the document looking for particular data markers (such as NOP, BPG, or PTX::TRN). If NOP, it sends to another subroutine that has different decoding functions. If PTX::TRN (transparent data), it sends it to this subroutine.
      Got it! Marshall gets the major thumbs up. I took out the "or next" line and it worked! It was going to next before any end of loop could be established.

      removed this:

      length( $x = $struct->{$key} ) or next;

      Thank you all!

      Peppi