Instead of thinking in terms of "removing lines," think of it as, "print the rest of the lines, skipping certain ones." Also, the array you read the file into is not the file. You have to print the results back out. The best method is usually to read line-by-line from the input file, printing to a new file as you go, then move that new file into place if necessary. So in pseudocode:

open inputfile for reading open outputfile for writing while get a line from inputfile split it into fields if the 8th field is >= -10 and <= 10 print the line to outputfile close the files move outputfile to inputfile

If, for some reason, you can't print the results to a new file (perhaps you don't have enough disc space to accommodate two copies of the data at once), look into the tie function and related modules. They allow you to tie an array variable to a file, so that each line is one element of the array. Then you could splice elements you don't want out of the array, and those lines will be removed from the file. But that method is more advanced, usually unnecessary, and more dangerous -- the "write to a new file then move it" method gives you a chance to make sure the new copy is what you wanted before wiping out the old copy.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.


In reply to Re: Remove line from file based on numeric column value by aaron_baugher
in thread Remove line from file based on numeric column value by Bama_Perl

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.