Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Comment on

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

Assuming that there are no _extra_ commas or equal signs besides the ones that separate key/value pairs and separate the keys from the values, ...

my @stuff; while (<FILEHANDLE>) { chomp; push @stuff, { map {split /\s*=\s*/} split /,\s*/ } }

But don't just copy this code if you don't understand it, especially if this is going into something you'll have to maintain. Let's break that down into something more verbose...

First, the {} constructs a reference to a hash, so the code above is equivalent to this:

my @stuff; while (<FILEHANDLE>) { chomp; my %record = map {split /\s*=\s*/} split /,\s*/; push @stuff, \%record; }

split uses $_ by default and produces a list, so we could expand the above as follows...

my @stuff; while (<FILEHANDLE>) { chomp; my @keysandvalues; my @field = split /,\s*/, $_; foreach $f (@field) { my ($key, $value) = split /\s*=\s*/, $_; push @keysandvalues, $key; push @keysandvalues, $value; } my %record = @keysandvalues; push @stuff, \%record; }

The last version is quite verbose, and when you're comfortable with references and list operators you'll probably not like it, but you may want to stick with something more verbose like that for now if you understand it better than the shorter version.

Also, be aware that if there are any anomalies in the input, such as any missing or extra commas or equal signs, this code will produce quite strange results. I believe there is an extra argument you can optionally pass to split to limit the number of pieces it will split things into, which may be of value here. I'd look it up, but my camel is at home and I'm not. Perhaps someone else can reply and confirm whether it is the third argument as ISTR.

update: Added \s* and chomp in several places to better trim whitespace.

$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/

In reply to Re: Parsing data into an Array of Hashes by jonadab
in thread Parsing data into an Array of Hashes by nimdokk

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 surveying the Monastery: (9)
    As of 2014-08-22 05:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The best computer themed movie is:











      Results (147 votes), past polls