You could do something with split like:

my @headers = split /\t/, <DATA>; while ( my $line = <DATA> ) { chomp $line; next if not $line; # skip blank lines. my ($name, $rank, $serial_num, $iq, $hit_points) = split /\t/, $line; # Do whatever it is you want to with this info... }

I used the <DATA> filehandle simply out of convenience for this example. You'll want to either use <> or open a handle to a particular external file using open.

I'm not sure what you meant by "the previous HTML file", so I can't comment on that part of your question.

Iterating over 2000 rows in a file to find one column in one row isn't an unreasonable thing to do unless you're going to be doing it so often that the script becomes IO bound. In which case, it would be better to move away from flat files and into the database world.

If you need to keep track of what line number you're on in the file, remember that the special variable, $. (as described in perlvar) always knows what line number you're looking at in a file. Of course the first line is the header, as you mentioned.

If the columns maintain the same dimensions from one line to the next, you can gain some speed efficiency by using unpack or substr instead of split. However, unpack and substr aren't going to allow for variable-width columns. If that's a possibility, split is the first choice.


Dave


"If I had my life to live over again, I'd be a plumber." -- Albert Einstein

In reply to Re: whats the best way to query/extract fields from a tab delimited file by davido
in thread whats the best way to query/extract fields from a tab delimited file by aussieaubs

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.