Re: file handling
by toolic (Bishop) on Jul 09, 2008 at 16:31 UTC
|
while (<>) {
print if $. > 2;
}
| [reply] [d/l] [select] |
|
use IO::File;
my $fh = IO::File->new( 'foobar.txt', 'r' );
while ( my $line = $fh->readline ) {
print $line if $fh->input_line_number > 3;
}
| [reply] [d/l] |
|
I like the different approach. But, I can not get your example to work. I get this error:
Can't locate object method "readline" via package "IO::File"
Admittedly, I am running an older version of perl (5.8.5 on linux), but if I look at the docs for the more recent versions of perl (5.8.8 and 5.10.0) for the IO::File core module, they also do not mention a "readline" method. Does your code actually run for you? Just curious.
| [reply] [d/l] |
|
|
|
I personally believe that there are basically two approaches:
- one as yours, which involves doing something unnecessary -namely the check- even when it will boil down to a noop, and is aesthetically unsatisfying even if the actual impact on performance will be negligible;
- one which will skip two lines (if there are!) in advance and then proceed as usual, and is also aesthetically unsatisfying for its inherent unavoidable code duplication.
This kind of situation (as opposed to very specific one) has led me in the past to many "doing it only once" kinda questions both in 5 and 6 realms. (The search is suboptimal, BTW, but should shed some light.) Eventually, for the most common case, TimToady just said that as far as Perl 6 is concerned, it may be a matter of compiler optimization behind the curtains, and that's fine for me. Whatever, in Perl 5 instead I can't help but feeling that there's something missing, however small, negligible, unimportant the practical problem may be. (But that's just me, I know...)
(Apologies for replying so late.)
| [reply] [d/l] |
Re: file handling
by polettix (Vicar) on Jul 09, 2008 at 16:32 UTC
|
Please see page 42 of the "Homework Exercise Answer Book". Meanwhile, you can take a look at the following interesting node: How do I post a question effectively?.
perl -ple'$_=reverse' <<<ti.xittelop@oivalf
Io ho capito... ma tu che hai detto?
| [reply] |
Re: file handling
by shoness (Friar) on Jul 09, 2008 at 17:48 UTC
|
/usr/bin/sed -n '3,$ p' < your_input_file
| [reply] [d/l] |
|
/usr/bin/tail +3 your_input_file
| [reply] [d/l] |
|
perl -e '@a=<>; print @a[3..$#a]'
| [reply] [d/l] |
|
Re: file handling
by friedo (Prior) on Jul 09, 2008 at 17:04 UTC
|
How to ask question using common sense that's not your homework | [reply] |
Re: file handling
by cdarke (Prior) on Jul 10, 2008 at 08:32 UTC
|
Like sed and awk, Perl has line addressing: while (<>)
{
print if 3..eof
}
| [reply] [d/l] |
Re: file handling
by apl (Monsignor) on Jul 09, 2008 at 17:17 UTC
|
| [reply] |