Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

one-line summations in perl.

136 views
Skip to first unread message

Felix Lee

unread,
Nov 5, 1991, 7:58:44 PM11/5/91
to
awk is much better than perl at one-line summations:
awk '{t += $1} END {print t}'

With perl you can say
perl -ane '$t += $F[0]'
but you have no way of tacking on an END clause. You have to write it
out longhand:
perl -e 'while (<>) { @F = split; $t += $F[0]; } print $t'

Perhaps it would be nice if the -n flag only applied to one -e
argument, so you could say
perl -ane '$t += $F[0]' -e 'print $t'
but this may conflict with existing practice.

A different flag, like -E, for end clauses may be better:
perl -ane '$t += $F[0]' -E 'print $t'
and perhaps a -B for begin clauses.

But this is still clumsier than awk. The reason to use perl rather
than awk is because perl has builtins that let you conveniently do
things like hex sums. But other than that, awk seems much more suited
to columnar applications.
--
Felix Lee fl...@cs.psu.edu

Chris Eich

unread,
Nov 6, 1991, 12:50:29 PM11/6/91
to

With perl you can say
perl -ane '$t += $F[0]'
but you have no way of tacking on an END clause.

What's wrong with this (inefficient, perhaps?):

perl -ane '$t += $F[0]; print $t if eof()'

Chris

Felix Lee

unread,
Nov 9, 1991, 8:18:15 AM11/9/91
to
Larry Wall wrote:
>Seriously, it's durn near impossible for one language to be better than
>another language at absolutely everything.

I won't argue with that. (At least, not yet:-)

The reason I originally brought up this topic was I noticed that I
rarely use perl at the command line. awk often, perl never. On the
other hand, I rarely write awk scripts. perl often, awk never.

This was somewhat surprising, given the reputation that perl has for
one-liners.

Perhaps it's just personal habit. Using dc rather than perl -de 0.
Using xargs rm rather than perl -lne unlink. Using sed -e rather than
perl -pe. (It slices, it dices, it even juliennes!)

If there's something more substantial that I need to use perl for,
then I toss a script in a temporary file, where I can consider and
revise.

Ah. Perhaps it's because I don't use a shell with editing. Someday
I'll find a nice shell and editor and see how my habits change.
--
Felix Lee fl...@cs.psu.edu

Larry Wall

unread,
Nov 7, 1991, 3:05:04 PM11/7/91
to
In article <l8bHn=?l...@cs.psu.edu> fl...@cs.psu.edu (Felix Lee) writes:
: awk is much better than perl at one-line summations:

: awk '{t += $1} END {print t}'

Nit pick: that's a syntax error in nawk. You need a newline before the END.

: With perl you can say


: perl -ane '$t += $F[0]'
: but you have no way of tacking on an END clause. You have to write it
: out longhand:
: perl -e 'while (<>) { @F = split; $t += $F[0]; } print $t'

or,

perl -le '$t += (split)[0] while <>; print $t'

or, taking a runtime hit,

perl -lane '$t += $F[0]; print $t if eof()';

: Perhaps it would be nice if the -n flag only applied to one -e


: argument, so you could say
: perl -ane '$t += $F[0]' -e 'print $t'
: but this may conflict with existing practice.

I suspect it would break things.

: A different flag, like -E, for end clauses may be better:


: perl -ane '$t += $F[0]' -E 'print $t'
: and perhaps a -B for begin clauses.

I've considered doing that more than once, but it sacrifices a lot of
readability for very little brevity.

: But this is still clumsier than awk. The reason to use perl rather


: than awk is because perl has builtins that let you conveniently do
: things like hex sums. But other than that, awk seems much more suited
: to columnar applications.

Hey, I had to let awk be better at *something*... :-)

Seriously, it's durn near impossible for one language to be better than

another language at absolutely everything. My favorite language is
call STAR. It's extremely concise. It has exactly one verb '*', which
does exactly what I want at the moment. You know, kinda like the
control box for Gigantor.

Larry

0 new messages