Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by Random_Walk (Parson)
on Oct 10, 2008 at 11:36 UTC ( #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!


Comment on Re: Generate the perl sequence 1, 11, 111, ....
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (18)
As of 2014-10-21 14:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (105 votes), past polls