Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

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

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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?

    What's my password?
    Create A New User
    LanX zooming holli s picture seeking onion
    [holli]: Oh, seems like LanX' team has won the fourth World Cup ... awesome!
    LanX grins
    [marto]: anyway, don't come to Glasgow for the football :)
    [holli]: the hell? i have a scriptblocker on.
    [holli]: how do you update a different window without javascript?
    [holli]: ah, chatterbox right
    [marto]: it's a normal HTML form, with some hidden input fields
    [holli]: forgot that thing exists for a moment.
    LanX welcome to the twilight zone ...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (14)
    As of 2017-12-14 16:14 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (398 votes). Check out past polls.