Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

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

by johndageek (Hermit)
on Oct 10, 2008 at 15:38 UTC ( #716480=note: print w/replies, xml ) Need Help??

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

just another simple minded shot:
$a=1; while ($b< 100){ $a =~ s/1$/11/; print "$a\n"; $b++; }


Replies are listed 'Best First'.
Re^2: Generate the perl sequence 1, 11, 111, ....
by blazar (Canon) on Oct 11, 2008 at 11:03 UTC

    I personally believe that

    • a "better" s-based solution was already posted by jwkrahn, in particular:
    • there's no need to match a 1 at the end of the string, and avoiding to do so could generalize the code to spit out the sequence to begin with zero 1's - not that anybody requested it, but it's such a tiny violation of YAGNI for both a simpler syntax and added flexibility that I would only avoid it if it were expressedly ruled out;
    • we recommend not to use $a and $b as general purpose variables all the time, which would be especially important here, given that the OP is clearly a newbie;
    • this thread is clearly facetious in many ways, but as a general programming rule, it's awkward and error prone that you somehow unnecessarily duplicate information, holding it in two variables where one would suffice: namely in $a as the length of $a itself and in $b as an integer, whereas the two quantities are fundamentally the same. Once you use only one variable, perhaps taking advantage of $_ to use smart defaults, and switching to the while statement modifier, your code could be morphed into:
    perl -le 's/$/1/, print while 100 > length'

    Incidentally, now that I notice, the anchor is not necessary at all. If it were not for a mandatory /e modifier, this could make for a golfed solution just as good as JavaFan's

    perl -E'{s//say/e,redo}'

    but that's not the case...

    If you can't understand the incipit, then please check the IPB Campaign.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://716480]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2018-04-21 20:26 GMT
Find Nodes?
    Voting Booth?