Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Here's one way. The iterator in this example is very simple: it has no way to reset or reinitialize once it is exhausted, and it will only reach exhaustion in a while-loop or equivalent (update: i.e., in scalar-context invocation).

c:\@Work\Perl>perl use strict; use warnings; # use Data::Dump qw(dd); # for debug sub Iterator (&) { return $_[0]; } # syntactic sugar per mjd (Dominus +) sub irange_limited { my ($start, $end) = @_; return Iterator { return if $start > $end; return wantarray ? $start .. $end : $start++; }; } my $iter = irange_limited(3, 5); for my $n ($iter->()) { print "for loop: $n \n"; } while (my $n = $iter->()) { print "while loop: $n \n"; } ^Z for loop: 3 for loop: 4 for loop: 5 while loop: 3 while loop: 4 while loop: 5

Update: Here's a slightly more sophisticated iterator. It always returns the full range when invoked in list context, and it will automatically reset when exhausted upon invocation in scalar context. Note, however, that list and scalar context invocations are independent of each other!

sub irange_limited { my ($start, $end) = @_; my $n = $start; return Iterator { return wantarray ? $start .. $end : $n > $end ? ($n = $start, ()) : $n++ ; }; }
(Update: Also note that this solution has a semipredicate problem. The range -1, 1 will result in a value of 0 (false) that will terminate a while-loop prematurely. A further defined test is needed in the while-loop condition because undef (also false) flags iterator exhaustion. (Update: There are also other solutions to this problem :))

Give a man a fish:  <%-{-{-{-<

In reply to Re: How can I create an iterator that works inside foreach() (updated) by AnomalousMonk
in thread How can I create an iterator that works inside foreach() by PUCKERING

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2023-03-21 01:05 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (59 votes). Check out past polls.