For Part 1 try the following snippet which calls the unix tail function:
system("tail -10 inputFile.txt >> outputFile.txt"); open <FILE, "</path/to/outputFile.txt"> or die "can't open file: $!\n" +; #do whatever here close FILE or die "can't close file $!\n";

In general, I do rely on calling Unix command line utilities for tools that I don't want to reinvent. I don't work on PC so much, so I'm not familiar with the situation there.

A 'pure' Perl implementation might involve counting all the lines in a file and then using the count to generate a new file which you would then process. This is more a Part 2ish approach. So for Part 2, try something like the following:

@input = <FILE>; $i = 0; foreach ($line (@input)) { $i++; if ($i >= 1034 || $i <= 1047) { #process here } } close FILE or die "can't close file: $!\n";

To count all the lines, just count your way through the foreach loop and use the final $i value as the number of lines in your file. This should help, I hope.

Have a look at chapters 8 and 9 of the Perl Cookbook - I just noticed a simple and elegant way of doing the line counting there. You might also try working with this PerlMonks node: Parsing a file one line at a time. I found this one to be quite helpful for this type of problem in general.

Good luck!

MadraghRua
yet another biologist hacking perl....


In reply to Re: Simulating UNIX's "tail" in core Perl by MadraghRua
in thread Simulating UNIX's "tail" in core Perl by gryphon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.