Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: adding items, but with interim results

by LanX (Chancellor)
on Mar 31, 2013 at 00:24 UTC ( #1026310=note: print w/replies, xml ) Need Help??


in reply to Re^2: adding items, but with interim results
in thread adding items, but with interim results

> but it presents a stumbling block to which it is perhaps unwise to expose a novice

I second this!

Especially because state has some implementation issues besides needing to be activated with use feature.

a simple my $z outside the map has the same effect and is backwards compatible.

DB<104> @x=1..4 => (1, 2, 3, 4) DB<105> use feature state; my @y = map { state $z = 0; $z += $_ } @x +; => (1, 3, 6, 10) DB<106> my $z; my @y = map { $z += $_ } @x; => (1, 3, 6, 10)

and if the scope of $z is of importance, use blocks to limit it.

DB<107> my @y; { my $z; @y = map { $z += $_ } @x; } => (1, 3, 6, 10) DB<109> my @y = do { my $z; map { $z += $_ } @x } => (1, 3, 6, 10)

Cheers Rolf

( addicted to the Perl Programming Language)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1026310]
help
Chatterbox?
[GotToBTru]: yes, it turns out I am
[LanX]: http://perldoc. perl.org/perlsub. html#Prototypes
[GotToBTru]: that page, strangely enough, never mentions that construct
[LanX]: ; is for optional prototypes
[LanX]: & is codeblock @ array
[GotToBTru]: okay, that is there in the text, not in literal use in any of the examples
[GotToBTru]: the text suggest it is redundant before @
[LanX]: propably ... I also notice new stuff

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (9)
As of 2017-01-18 21:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you watch meteor showers?




    Results (164 votes). Check out past polls.