Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: adding a column of integers

by sintadil (Pilgrim)
on Sep 02, 2004 at 18:14 UTC ( [id://388031]=note: print w/replies, xml ) Need Help??


in reply to adding a column of integers

perl -le 'map{$total += $_} <>; print $total'

Replies are listed 'Best First'.
Re^2: adding a column of integers
by sintadil (Pilgrim) on Sep 02, 2004 at 18:16 UTC

    Note that this uses map in void context, which is bad except in newer Perls. It's not so horrible for a one-liner, though.

      My, my, my. Another fool who thinks he can appear to be smart by chiming the familiar song about map in void context. And to be really smart he mentions it's not so bad in modern perl.

      Forget about the map. The damage is done before the map is called. Executing <> in list context is slurping in the entire file. You've lost your constant memory solution right there. Regardless of the version of Perl you are using.

      I would use a grep instead of a map... :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-19 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found