stevieb has asked for the wisdom of the Perl Monks concerning the following question:
Helped a fellow Perler with a problem on a lesser Perl site, and am looking for feedback on my code. It's late in the day, and somehow I think this can be written shorter and better, but I can't wrap my head around how at the moment. I'm looking for how to make this code shorter and/or more efficient (examples using modules welcome).
The request was to take data like this:
NAME|AGE|CITY|ZIP AAA|23|STAT|60001 BBB|34|PPOR|12345 CCC|11|TRET|2345
...then print out a sub-selection of columns in the same format so the output was like this (from cols NAME & CITY):
NAME|CITY AAA|STAT BBB|PPOR CCC|TRET
Here is my code that I'm having difficulty thinking about how to make smaller/better (requires perl v5.12+ due to the use of each() on the array):
#!/usr/bin/perl use warnings; use strict; use List::Util qw( first ); my @master_cols = qw( NAME CITY ); my @results; my %positions; while ( my $line = <DATA> ){ chomp $line; my @row = split /\|/, $line; if ( $. == 1 ){ while ( my ( $i, $elem ) = each @row ){ if ( first { $elem eq $_ } @master_cols ){ $positions{ $elem } = $i; } } } my @row_items; for my $pos ( sort values %positions ){ push @row_items, $row[ $pos ]; } push @results, [ @row_items ]; } for my $result ( @results ){ print "\n"; print join '|', @{ $result }; } __DATA__ NAME|AGE|CITY|ZIP AAA|23|STAT|60001 BBB|34|PPOR|12345 CCC|11|TRET|2345
ps. I've never used any csv-type modules, but I have heard of their use, so if I've overlooked their use for this, feel free to point one out and be harsh on me ;)
Update: I should have clarified... in the code above, if I opt to select different column headers (eg ZIP, AGE, NAME as opposed to NAME, CITY), the order is preserved regardless of the column name or the order passed in via the @master_cols array. I'm sorry this wasn't clear originally. It doesn't take away from the quality responses so far though.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Better method to cut columns from delimited file
by BrowserUk (Patriarch) on May 10, 2012 at 04:47 UTC | |
by stevieb (Canon) on May 10, 2012 at 04:56 UTC | |
by NetWallah (Canon) on May 10, 2012 at 05:21 UTC | |
by BrowserUk (Patriarch) on May 10, 2012 at 05:33 UTC | |
|
Re: Better method to cut columns from delimited file
by Marshall (Canon) on May 10, 2012 at 07:48 UTC | |
|
Re: Better method to cut columns from delimited file
by roboticus (Chancellor) on May 10, 2012 at 10:39 UTC | |
|
Re: Better method to cut columns from delimited file
by Tux (Canon) on May 10, 2012 at 13:37 UTC | |
|
Re: Better method to cut columns from delimited file
by stevieb (Canon) on May 10, 2012 at 08:18 UTC |