I might as well wade into the fray, and show TIMTOWTDI in Perl5...

How about a function table where the entries remove themselves once they're no longer needed?

#!/your/perl/here # demo self-modifying function table use strict; use warnings; sub gen_sub { my ($match, $target) = @_; my $count = 0; return sub { my ($item, $function, $num) = @_; print "<$match> eq <$item>?\n"; if ($item eq $match) { $count++; print "found $match, count=$count, target=$target\n"; if ($count >= $target) { delete( $function->{$num} ); print "no more $match\n"; } return(1,$function); } return(0,$function); } # end sub } # gen_sub # construct function table my $function = {}; my $limit = 1; my $order = 0; foreach my $sub ('foo', 'bar', 'baz') { $function->{$order++} = { 'NAME' => $sub, 'CODE' => gen_sub($sub,$limit++) }; } my $hit; PASS: while (keys %{$function}) { while (<DATA>) { chomp; foreach my $num (sort {$a<=>$b} keys %{$function}) { ($hit,$function) = $function->{$num}{CODE}($_,$function,$n +um); next PASS if $hit; } } } exit; __DATA__ junk foo bar baz foo bar baz foo bar baz more junk __OUTPUT__ <foo> eq <junk>? <bar> eq <junk>? <baz> eq <junk>? <foo> eq <foo>? found foo, count=1, target=1 no more foo <bar> eq <bar>? found bar, count=1, target=2 <bar> eq <baz>? <baz> eq <baz>? found baz, count=1, target=3 <bar> eq <foo>? <baz> eq <foo>? <bar> eq <bar>? found bar, count=2, target=2 no more bar <baz> eq <baz>? found baz, count=2, target=3 <baz> eq <foo>? <baz> eq <bar>? <baz> eq <baz>? found baz, count=3, target=3 no more baz
Once there is no more code that would pass the conditionals, the loop exits.

(BTW, what keeps the world from imploding when the current sub is deleted from the function hash? There must be a referent lying around somewhere until the sub exits, right?)

-QM
--
Quantum Mechanics: The dreams stuff is made of


In reply to Re: Doing "it" only once by QM
in thread Doing "it" only once by Limbic~Region

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.