[Novalug] Tutorials
Rob Payne
rnspayne at the-paynes.com
Sun Dec 17 09:06:20 EST 2006
On Sat, Dec 16, 2006 at 10:06:43PM -0500, Kevin Dwyer wrote:
> On Sat, Dec 16, 2006 at 07:38:07PM -0500, Rob Payne wrote:
> > On Sat, Dec 16, 2006 at 01:07:40PM -0500, Kevin Dwyer wrote:
> > > On Sat, Dec 16, 2006 at 12:17:30PM -0500, James Ewing Cottrell 3rd wrote:
> > > > For example, the "print the second field" script above can be expressed
> > > > as a one-liner in Perl as: ps -ef | perl -lane 'print $F[1]'
> > >
> > > This is precisely what keeps me from ever writing things in perl.
> > >
> > > I.E.: Just where the hell did $F come from? I guess $_ was already
> > > spoken for.
> >
> > Well, since this is a thread on tutorials, maybe you can offer some
> > advice instead of just criticism? If you prefer another language,
> > maybe offer some code in that one.
>
> It can be done in any number of languages. perl is just full of
> shortcuts so that it can fit in one-line.
>
> Here's python in one line:
> ps -ef | python -c 'import sys; print "\n".join([line.split()[1] for line in sys.stdin])'
Thanks, I didn't know the incantation to make python do it in
one-line.
> Python isn't meant to be written in one line though, which is why that's
> ugly. Something better would look like this:
As you said, you can make perl do it in a similar fashion. I don't
generally resort to one-liners because if I'm scripting something I'll
generally have to do it more than once. If I need to do it again,
having to re-write the one-liner is a waste. Here's more like how I'd
do it in perl:
#!/usr/bin/perl
use strict;
use warnings;
my($line) = '';
while ($line = <STDIN>)
{
if (defined ($line))
{
my(@fields) = split(/\s+/, $line);
print "@fields[1]\n";
}
}
> And you could make perl look like that too, but no one does because it's
> some sort of faux pas. (Perl people will say: Why bother when I can do
> it in one line? So it's a perspective thing, or perhaps a function of
> how deranged one's mind is.)
I'm just a different type of deranged perl person.
-rob
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://calypso.tux.org/pipermail/novalug/attachments/20061217/0b143cb6/attachment.bin
More information about the Novalug
mailing list