$ cat plonk.pl #!/usr/bin/perl use strict; use warnings; my @wanted = qw(CITY NAME); # get map of column headings (COLNAME=>INDEX) my %headings = do { my $t = ; chomp $t; my $cnt=0; map { $_=>$cnt++ } split /\|/, $t; }; while () { chomp; my @cols = split /\|/; # A hash slice to get the column numbers we want, and # an array slice to extract those columns from the # list.... print join(", ",@cols[@headings{@wanted}]),"\n"; } __DATA__ NAME|AGE|CITY|ZIP AAA|23|STAT|60001 BBB|34|PPOR|12345 CCC|11|TRET|2345 $ perl plonk.pl STAT, AAA PPOR, BBB TRET, CCC