Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Issue parsing CSV into hashes?

by ikegami (Patriarch)
on Sep 27, 2010 at 20:45 UTC ( [id://862295]=note: print w/replies, xml ) Need Help??


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

       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.)

    Almost true. It didn't work with eol's not having a trailing \r or \n, which was caused by the underlying implementation that used perl's internal getline () mechanism without modifying $/ locally.

    I plan to release version 0.74 this week, some documentation changes are pending. The _PP counterpart is also ready as we speak.

    ikegami, you should start using the auto_diag attribute :)


    Enjoy, Have FUN! H.Merijn

      Perl doesn't know or care about "\r", so it must be a special case. I tested it, revealing that "\r" was broken and still is. I reopened the ticket.

      auto_diag: nice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://862295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (3)
As of 2024-04-25 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found