Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Re: Re: "foreach" is to "next" as "map" is to ???

by sacked (Hermit)
on May 26, 2004 at 16:45 UTC ( [id://356642]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: "foreach" is to "next" as "map" is to ???
in thread "foreach" is to "next" as "map" is to ???

I meant "equivalent" in the sense that @odds is the same after both the map and the foreach.

As far as equivalent code, I'd argue that this is closer to the map than your for loop using unless:
my @odds; for ( 0 .. 10 ) { push @odds => $_ % 2 ? $_ : (); }
In the same way that the original foreach using next does not evaluate the push for every iteration, your code using push/unless does not evaluate the push every iteration.

--sacked

Replies are listed 'Best First'.
Re: Re: Re: Re: "foreach" is to "next" as "map" is to ???
by Limbic~Region (Chancellor) on May 26, 2004 at 16:50 UTC
    sacked,
    Maybe I am just nit picking, but if you meant equivalent results you should not have said equivalent code.
    for ( @lines ) { next if /^#/; # ... a dozen lines of code push @paragraph, $_; }
    There is no sane way to do the equivalent of this in a map block. You can only choose not to provide the line to the paragraph at the last instruction.

    L~R

      There is no sane way to do the equivalent of this in a map block.
      Of course there is.
      map { !/^#/ ? () : do { # ... a dozen lines of code } } @lines

      Abigail

      You wrote:
      There is no sane way to do the equivalent of this in a map block.
      @paragraph= map { if( /^#/ ) { () } else { # ... a dozen lines of code $_ } } @lines;

      --sacked

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-25 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found