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.";

In reply to Re: whats the best way to query/extract fields from a tab delimited file by sauoq
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.