in reply to Re^2: parsing CSV
in thread parsing CSV
The code is pretty much the same except that the second page data gets new lines inserted in front of the id codes and we do a little clean up to remove white space at the ends of lines:
use strict; use warnings; use Text::CSV; my $page1 = <<PG1CSV; 512.45,c100 6734, c200 5653.2, c300 PG1CSV my $csv = Text::CSV->new(); my %idData; open my $pg1In, '<', \$page1; while (my $row = $csv->getline($pg1In)) { s/^\s+|\s+$//g for @$row; $idData{$row->[1]}{size} = $row->[0]; $idData{$row->[1]}{name} = '-- missing --'; } close $pg1In; my $page2 = <<PG2CSV; c100, Joe Shmo c200, Jack Black c300, Cinderella c400, Barack Obama c5 +00, Cruella Deville PG2CSV $page2 =~ s/\b(?=\w+,)/\n/g; # Insert newlines in front of id codes open my $pg2In, '<', \$page2; while (my $row = $csv->getline($pg2In)) { next if !$row->[0]; # Skip blank lines s/^\s+|\s+$//g for @$row; $idData{$row->[0]}{name} = $row->[1]; $idData{$row->[0]}{size} //= '-- missing --'; } close $pg2In; for my $id (sort keys %idData) { print "$id: $idData{$id}{name} size $idData{$id}{size}\n"; }
Prints:
c100: Joe Shmo size 512.45 c200: Jack Black size 6734 c300: Cinderella size 5653.2 c400: Barack Obama size -- missing -- c500: Cruella Deville size -- missing --
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: parsing CSV
by younggrasshopper13 (Novice) on Oct 07, 2016 at 05:01 UTC | |
by GrandFather (Saint) on Oct 07, 2016 at 06:23 UTC | |
by younggrasshopper13 (Novice) on Oct 07, 2016 at 07:03 UTC | |
by Corion (Patriarch) on Oct 07, 2016 at 08:04 UTC | |
by younggrasshopper13 (Novice) on Oct 07, 2016 at 14:25 UTC | |
| |
|
Re^4: parsing CSV
by younggrasshopper13 (Novice) on Oct 08, 2016 at 02:15 UTC | |
by AnomalousMonk (Archbishop) on Oct 08, 2016 at 03:13 UTC | |
by younggrasshopper13 (Novice) on Oct 08, 2016 at 17:42 UTC | |
by marto (Cardinal) on Oct 08, 2016 at 17:57 UTC | |
by younggrasshopper13 (Novice) on Oct 08, 2016 at 21:43 UTC | |
|