Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

file handling

by trio (Initiate)
on Jul 09, 2008 at 16:24 UTC ( [id://696492]=perlquestion: print w/replies, xml ) Need Help??

trio has asked for the wisdom of the Perl Monks concerning the following question:

How to print the content of file line by line starting from the 3rd line using loop

Replies are listed 'Best First'.
Re: file handling
by toolic (Bishop) on Jul 09, 2008 at 16:31 UTC
      using IO::File
      use IO::File; my $fh = IO::File->new( 'foobar.txt', 'r' ); while ( my $line = $fh->readline ) { print $line if $fh->input_line_number > 3; }


      Evan Carroll
      I hack for the ladies.
      www.EvanCarroll.com
        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.

      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.)

      --
      If you can't understand the incipit, then please check the IPB Campaign.
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?
Re: file handling
by shoness (Friar) on Jul 09, 2008 at 17:48 UTC
    Easier...
    /usr/bin/sed -n '3,$ p' < your_input_file
      Even easier:
      /usr/bin/tail +3 your_input_file
        Harder but -Ofun:
        perl -e '@a=<>; print @a[3..$#a]'
        []s, HTH, Massa
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
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 }
Re: file handling
by apl (Monsignor) on Jul 09, 2008 at 17:17 UTC
    grep -n . name_of_file

    Revised: I overlooked the restriction that the display start with line 3.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://696492]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found