in reply to Issue parsing CSV into hashes?
CSV files are two dimensional (lines of fields). Your data is also two dimensional. To parse your data as a CSV file, each hash element would need to be parsed as a line.
use strict; use warnings; use Data::Dumper qw( Dumper ); use Text::CSV_XS 0.74; # eol bug fix. my $csv = Text::CSV_XS->new({ binary => 1, sep_char => ':', eol => ',', }); while (<DATA>) { chomp; my %h; open(my $fh, '<', \$_) or die; while (my $row = $csv->getline($fh)) { $h{ $row->[0] } = $row->[1]; } $csv->eof or $csv->error_diag(); print(Dumper(\%h)); } __DATA__ "evt":"Login","time":"now","msg":"Login success, welcome back!"
$VAR1 = { 'msg' => 'Login success, welcome back!', 'time' => 'now', 'evt' => 'Login' };
One catch: eol doesn't work for anything but "\n" in 0.73, and 0.74 isn't out yet. (The bug has been fixed, but a release hasn't been created yet.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Issue parsing <span style=
by Tux (Canon) on Sep 27, 2010 at 21:38 UTC | |
by ikegami (Patriarch) on Sep 27, 2010 at 22:26 UTC | |
by ikegami (Patriarch) on Sep 27, 2010 at 22:37 UTC |