Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Generate the perl sequence 1, 11, 111, ....

by Random_Walk (Prior)
on Oct 10, 2008 at 11:36 UTC ( [id://716432]=note: print w/replies, xml ) Need Help??


in reply to Generate the perl sequence 1, 11, 111, ....

As this infinite series could get rather large you may like to create an iterator to return the next member from the series each time it is called

#!/usr/bin/perl use strict; use warnings; # make our iterator (a subroutine which holds its own state) my $ones = seq_gen(1); while (1) { # call iterator and print result my $next = $ones->(); print "$next\n"; sleep 1; } sub seq_gen { # generate and return a subroutine that captures its entry values +and # gives you the next member each time it is called my $seq = shift; my $string = ''; return sub { $string .= $seq; return $string; } }

Cheers,
R.

Pereant, qui ante nos nostra dixerunt!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2024-04-18 11:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found