element addition

S

sghani73

I have the following output from a command...Can I pipe it to a single

line perl command to get the total (add each of them)....

4352
4160
1040
2112
8448
1040
2112
8448
1040
4224
2176


so perl output should be equal to 39152...


Thanks
 
A

A. Sinan Unur

(e-mail address removed) wrote in
I have the following output from a command...Can I pipe it to a
single line perl command to get the total (add each of them)....

What have you tried and how doesn't it work?

Sinan
 
P

Paul Lalli

I have the following output from a command...Can I pipe it to a single

line perl command to get the total (add each of them)....

4352
4160
1040
2112
8448
1040
2112
8448
1040
4224
2176

Yes.

Well, *I* can anyway. I don't actually know if *you* can, because I
don't know how much Perl you know. Perhaps if you made an attempt and
showed us what you tried and explain how it failed to meet your goals,
we could help you figure out where you went wrong.

Paul Lalli
 
R

robic0

Yes.

Well, *I* can anyway. I don't actually know if *you* can, because I
don't know how much Perl you know. Perhaps if you made an attempt and
showed us what you tried and explain how it failed to meet your goals,
we could help you figure out where you went wrong.

Paul Lalli

NO you can't, I know this for a fact! I know you pile on and I think I'm
gonna *pile* on you for a while!!!

Robic0
 
J

John W. Krahn

I have the following output from a command...Can I pipe it to a single

line perl command to get the total (add each of them)....

4352
4160
1040
2112
8448
1040
2112
8448
1040
4224
2176


so perl output should be equal to 39152...

$ echo "4352
4160
1040
2112
8448
1040
2112
8448
1040
4224
2176" | perl -pe'$\+=$_}{'
39152



John
 
T

Tad McClellan

I have the following output from a command...Can I pipe it to a single

line perl command to get the total (add each of them)....

4352
4160
1040
2112
8448
1040
2112
8448
1040
4224
2176


so perl output should be equal to 39152...


cat data | perl -lne '$n+=$_; END{print $n}'
 
U

Uri Guttman

TM> cat data | perl -lne '$n+=$_; END{print $n}'

shame on you! you have just been nominated for a useless use of cat
award. i am appalled! and this is a nicer one liner IMO.

perl -MList::Util=sum -le 'print sum <>'

uri
 
D

DJ Stunks

John said:
$ echo "4352
4160
1040
2112
8448
1040
2112
8448
1040
4224
2176" | perl -pe'$\+=$_}{'
39152

very nice :)

I didn't know you could just add to $ORS and get a real number....

just so I understand -- was the '}{' necessary so the continue block
was associated with the noop block, thus only printing the sum once at
the end of the input?

C:\tmp>perl -MO=Deparse -pe"$\+=$_}{"
LINE: while (defined($_ = <ARGV>)) {
$\ += $_;
}
{
();
}
continue {
die "-p destination: $!\n" unless print $_;
}

C:\tmp>perl -MO=Deparse -pe"$\+=$_"
LINE: while (defined($_ = <ARGV>)) {
$\ += $_;
}
continue {
die "-p destination: $!\n" unless print $_;
}

-jp
 
R

Rick Scott

([email protected] uttered:)
I have the following output from a command...Can I pipe it to a
single line perl command to get the total (add each of them)....

4352
4160
1040
2112
8448
1040
2112
8448
1040
4224
2176

so perl output should be equal to 39152...

data | perl -0ne 'print `clisp -q -x "(+ $_)"`'



=)
Rick
 
T

Tad McClellan

Uri Guttman said:
TM> cat data | perl -lne '$n+=$_; END{print $n}'

shame on you! you have just been nominated for a useless use of cat
award. i am appalled!


I knew that when I wrote it (of course).

The OP said he wanted to "pipe the data from a command" but didn't
give the name of the command, so I had to make one up.

So this UOC is useful as a proxy for whatever the actual command is.

and this is a nicer one liner IMO.

perl -MList::Util=sum -le 'print sum <>'


Yes, that is nicer.
 
X

xhoster

Uri Guttman said:
TM> cat data | perl -lne '$n+=$_; END{print $n}'

shame on you! you have just been nominated for a useless use of cat
award. i am appalled!

In this case, it is a place-holder for an unspecified other command. Quite
useful. And in general, use of cat in this manner can keep the data flow
in a nice left to right direction through a string of piped-together
commands, and also can make modification of the pipeline easier. Not
strictly necessary, perhaps, but far from useless.
and this is a nicer one liner IMO.

perl -MList::Util=sum -le 'print sum <>'

I prefer the first one. This one slurps all the numbers into memory.

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top