in reply to whats the best way to query/extract fields from a tab delimited file

The Text::CSV module on CPAN might be useful to you. CSV stands for "character separated values" and that's exactly what you have. (Most CSV files have headers too.) If you are going to work with this sort of data a lot, I highly recommend getting the module. If you have a lot of data, you might want to look at Text::CSV_XS as well. It's essentially the same thing but written in C for speed.

If all you are ever going to do is print columns, this should help get there:

#!/usr/bin/perl -w use strict; my $datafile = shift; my $want_col = shift; open my $fh, '<', $datafile or die "$datafile: $!"; my $headings = <$fh>; chomp $headings; my @columns = split /\t/, $headings; my %column; @column{ @columns } = 0 .. $#columns; die "Column '$want_col' not in $datafile" unless exists $column{ $want +_col }; while ( <$fh> ) { chomp; print join "\t", (split /\t/)[ $column{ $want_col } ], "\n"; }

You can put that in a file, make it executable, and call it as script.pl file.dat columnname. Modify it as needed.

-sauoq
"My two cents aren't worth a dime.";
  • Comment on Re: whats the best way to query/extract fields from a tab delimited file
  • Download Code