Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How To: Make An Iterator

by Limbic~Region (Chancellor)
on Apr 25, 2005 at 17:20 UTC ( [id://451278]=perltutorial: print w/replies, xml ) Need Help??

Help for this page

Select Code to Download


  1. or download this
    sub gen_iterator {
        my @initial_info = @_;
    ...
            return $current_state = $next_state;   
        };
    }
    
  2. or download this
    my $next = gen_iterator( 42 );
    while ( my $item = $next->() ) {
        print "$item\n";
    }
    
  3. or download this
    my @DNA = qw/A C T G/;
    my $seq = gen_permutate(14, @DNA);
    ...
            return join '', map { $list[ $_ ] } @curr;
        };
    }
    
  4. or download this
    my $start = $ARGV[0] || 999999;
    my $next_id = gen_id( $start );
    ...
        $tot += $_ for split //, $chk;
        return $tot % 10 == 0 ? 1 : 0;
    }
    
  5. or download this
    my $next_file = rotate( qw/FileA FileB FileC/ );
    print $next_file->(), "\n" for 1 .. 10;
    ...
            return $list[ $index ];
        };
    }
    
  6. or download this
    while ( my $pass = $next_pw->() ) {
        if ( unlock( $pass ) ) {
    ...
        };
    }
    sub unlock { $_[0] eq 'john' }
    
  7. or download this
    my @list = map { my @lines = <$_>; \@lines } @f_handles;
    # becomes
    ...
            return \@lines;
        };
    }
    
  8. or download this
    sub infinite_evens {
        my $curr = shift;
    ...
    }
    my $next_even = infinite_evens( 41 );
    print $next_even->(), "\n" while 1;
    

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perltutorial [id://451278]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-16 05:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found