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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |