[TriLUG] Perl question. . .
Brian Henning
Brian.Henning at datadirect.com
Tue Feb 17 15:29:03 EST 2009
I think what you really meant was this:
]$ cat bin/pick
#!/usr/bin/perl
my $pick = shift;
while(<>) {
if($pick-- == 0) {
print $_;
exit(0);
}
}
...which is of course what I really meant the first time.. :-)
~B
-----Original Message-----
From: trilug-bounces at trilug.org [mailto:trilug-bounces at trilug.org] On
Behalf Of Michael Peters
Sent: Tuesday, February 17, 2009 3:23 PM
To: Triangle Linux Users Group General Discussion
Subject: Re: [TriLUG] Perl question. . .
Michael Peters wrote:
> Michael Peters wrote:
>
>> if( $line++ == $pick ) {
>
> Actually that should probably be "++$line == $pick". Since most people
think of the first line as
> #1, not #0.
Wow, I really should have actually run that code first :) Here's a
better version:
]$ cat bin/pick
#!/usr/bin/perl
my $pick = shift;
my $count = 0;
while (my $line = <>) {
if( ++$count == $pick ) {
print $line;
exit(0);
}
}
--
Michael Peters
Plus Three, LP
--
TriLUG mailing list :
http://www.trilug.org/mailman/listinfo/trilug
TriLUG FAQ : http://www.trilug.org/wiki/Frequently_Asked_Questions
More information about the TriLUG
mailing list