Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Wait, I've not replied to this yet? Anyway, lots of options seem to be missing. Here are some more possibilities, no doubt there are more.

  • recursion with named subs: $_=0;sub f{say;$_++<9and f()}f;
  • recursion with subrefs: my$s;$s=sub{say$_[0];$_[0]<9and&$s(1+$_[0])};&$s(0);$s=0;
  • recursion via overload, tie, DESTROY, DB, SIG, or other automagical sub calls: $_=0;DESTROY{say$_;$_++<9and bless[]};bless[];
  • bounded looping without recursion: $_=0;sub f0{say;$_++<9}eval join"",map{"sub f".(1+$_)."{f$_ and f$_}"}0..7;f8();
  • recursion the nice way (ie. without circular references ie. without letrec or mutable variables): my$s=sub{say$_[0];$_[0]<9and&{$_[1]}(1+$_[0],$_[1])};&$s(0,$s);
  • redo: $_=0;{say;$_++<9and redo};
  • map: map say,0..9;
  • grep: grep say,0..9;
  • sort: ()=sort{$u{$a}++or say$a;$u{$b}++or say$b;0}0..9;
  • repeated substitution: $k=0;$_="a"x10;s/./say$k++/ge;
  • (?:{}) with list context g regex or the regex engine itself looping: $k=0;()=("a"x10)=~/.(?{say$k++})/g;
  • string eval: $_=0;eval "say(\$_++);"x10; (don't laugh, I heared the developers of the J programming language used this in the very early stage of creating J when it didn't yet have the goto or other control statements)
  • own loop library implemented in XS

Update 2010-10-17: see also the other poll If I was forced to use only one kind of loop for the rest of my days it would be a.


In reply to Re: My favorite looping mechanism in Perl is: by ambrus
in thread My favorite looping mechanism in Perl is: by vroom

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found