At 22:56 22/06/00, Niall O Broin wrote:
>On Thu, Jun 22, 2000 at 08:03:14PM +0100, Fergal Daly wrote:
>> > perl -ne '<>;print' filename
> > perl -ne 'print;<>' filename
> > Hmm... just tested those last 2 and they both print only the odds, what's
> > going on there?
>>In both of the above examples, the code which is executed prints $_ (print)
>and reads one more line from the input source (<>) - it doesn't matter which
>order they're done in - the net result is to print the odd line, and slurp
>in the even. You may be assuming that <> in scalar context is equivalent to
>$_ = <> but t'ain't so. To print the even lines, you could use
>>perl -ne 'print $a = <>' filename
Yep, that's what I was assuming. Of course it only does this when the <> is
the condition on a while loop, how could I have been so foolish ;-). This
is exactly why none of the Perl I've written for years (except for quick
one liners and pissing contests) has gone anywhere near $_, it's also why
Perl has such a bad name for readability.
> > I don't think this'll be beat
> > perl -pe '<>' filename
>>No, I don't suppose it will :-) and it certainly won't be beat for obscurity.
Didn't think it was too obscure, -p especially combined with -i (for in
place edit) is piece of magic everyone should know about. Anyway, it looks
like it was beaten on all counts
>this is 3 chars shorter :)
>>awk 'NR%2' filename
but counting the length of the executable name just isn't cricket and the
space between the e and the ' in mine is only there to enhance readability!
So I reckon it's really only 1 char shorter. If you'd really wanted it 3
chars shorter you should have left out the quotes, they're only necessary
for the perl version (did I win?:-))
Maintained by the ILUG website team. The aim of Linux.ie is to
support and help commercial and private users of Linux in Ireland. You can
display ILUG news in your own webpages, read backend
information to find out how. Networking services kindly provided by HEAnet, server kindly donated by
Dell. Linux is a trademark of Linus Torvalds,
used with permission. No penguins were harmed in the production or maintenance
of this highly praised website. Looking for the
Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!