Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

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

Not as succinct as some of the other answers but possibly of interest.

$ perl -Mstrict -Mwarnings -E ' my @a = ( 1 .. 10 ); sub transform (&@); @a = transform { ( ++ $_ ) % 10 } @a; say qq{@a}; sub transform (&@) { map $_[ 0 ]->( $_ ), @_[ 1 .. $#_ ] }' 2 3 4 5 6 7 8 9 0 1 $

Update: Much more concise if you make the transform subroutine operate on $_ by default.

$ perl -Mstrict -Mwarnings -E ' my @a = ( 1 .. 10 ); sub transform (&); transform { ( ++ $_ ) % 10 } for @a; say qq{@a}; sub transform (&) { $_[ 0 ]->( $_ ) }' 2 3 4 5 6 7 8 9 10 11 $

Update 2: Just noticed that isn't working properly so will have to investigate why :-(

Update 3: Got it!

$ perl -Mstrict -Mwarnings -E ' my @a = ( 1 .. 10 ); sub transform (&); transform { $_ = ( ++ $_ ) % 10 } for @a; say qq{@a}; sub transform (&) { $_[ 0 ]->( $_ ) }' 2 3 4 5 6 7 8 9 0 1 $

Cheers,

JohnGG


In reply to Re: A better (ie.more concise) way to write this? by johngg
in thread A better (ie.more concise) way to write this? by BrowserUk

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 browsing the Monastery: (8)
As of 2024-04-16 09:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found