Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

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

by Random_Walk (Prior)
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; } }


Pereant, qui ante nos nostra dixerunt!

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2016-10-22 21:53 GMT
Find Nodes?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?

    Results (298 votes). Check out past polls.