Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hi,

I just read this thread again and saw your reply.

Assuming, that an empty string is not a valid value, I came up with this:

#! /usr/bin/perl use strict; use warnings; use Text::CSV_XS; my $csv = Text::CSV_XS->new({ binary => 1, allow_whitespace => 1, }) or die "Cannot use CSV: " . Text::CSV_XS->error_diag(); # for testing; in real world, open file and use that handle my $fh = \*DATA; my (%hash, @hdr); while ( my $row = $csv->getline( $fh ) ) { # header not yet defined? or 1st cell starts with '<' ==> use row +as header if ( !@hdr || $row->[0] =~ m/^</ ) { @hdr = @{$row}; next; } # otherwise try to process data else { for my $i ( 0 .. $#hdr ) { # only add those values which contain at least one charact +er # so: no "undef"s or empty strings in result # if empty strings are OK or wanted, try to replace length +() with defined() push @{ $hash{$hdr[$i]} }, ( length $row->[$i] ? $row->[$i +] : () ); } } } # check created data structure require Data::Dumper; $Data::Dumper::Sortkeys = 1; print Data::Dumper::Dumper( \%hash ); __DATA__ <A1>, <A2>, <A3> a1, aa1, aaa1 a2, , aaa2 a3, aa3 a4 <B1>, <B2> b1, bb1 b2, bb2 b3
That produced a result like this:
$VAR1 = { '<A1>' => [ 'a1', 'a2', 'a3', 'a4' ], '<A2>' => [ 'aa1', 'aa3' ], '<A3>' => [ 'aaa1', 'aaa2' ], '<B1>' => [ 'b1', 'b2', 'b3' ], '<B2>' => [ 'bb1', 'bb2' ] };

In reply to Re^4: Help parsing a complicated csv by linuxer
in thread Help parsing a complicated csv by rmfin730

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (6)
    As of 2014-12-29 09:03 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (185 votes), past polls