use strict; my @cols_to_print = qw(LAST_NAME FIRST_NAME); my @col_nbrs; while () { 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 #### Wall Larry Joy Bill Stallman Richard Gates Bill