Yet another version, slightly whackier:
$ 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 = <DATA>; chomp $t; my $cnt=0; map { $_=>$cnt++ } split /\|/, $t; }; while (<DATA>) { 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
Update: Improved the (pre-existing) comments a little.
...roboticus
When your only tool is a hammer, all problems look like your thumb.
In reply to Re: Better method to cut columns from delimited file
by roboticus
in thread Better method to cut columns from delimited file
by stevieb
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |