The following is admittedly ugly as sin but it allows you to choose columns and set their order -- just substitute tabs for spaces:

use strict; my @cols_to_print = qw(LAST_NAME FIRST_NAME); my @col_nbrs; while (<DATA>) { my $line = $_; if ($. == 1) { #1st line of input? my @col_names = split(/\s+/, $line); for my $x (0..$#cols_to_print) { for my $y (0..$#col_names) { if ($cols_to_print[$x] eq $col_names[$y]) { push @col_nbrs, $y; last; } } } } else { my @cols = split /\s+/, $line; @cols = @cols[@col_nbrs]; #slice into wanted column nbrs print "@cols\n"; } } __DATA__ CUST_ID FIRST_NAME LAST_NAME FAV_LANG 1 Larry Wall Perl 2 Bill Joy Java 3 Richard Stallman emacslisp 4 Bill Gates tinyBasic
Would render:
Wall Larry Joy Bill Stallman Richard Gates Bill

This helps if the column selections truly need to be dynamic. @cols_to_print could come from ARGV or other external vars.

There might be a more idomatic way of handling the sequence for the first line, but darned if I figure out what it is.

Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"

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