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

In reply to Re: Break perl foreach loop by ig
in thread Break perl foreach loop by pvecchio

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.