Wow... When I started working on this there were no posts. Now there's 3.

Well I'll post my own solution also, though I think I've already been outdone.

My approach is simple. First split on the space to an array & then iterate over it looking for '='

When it's found, assign to that key. Every value that follows is assumed to belong to that key until another is mentioned.

You did say the purpose here was to assign multiple values an an individual key, yes?

Well then assuming that your values don't contain commas, you could do something similar when extracting. Test for a ',' & if it exisits, split on it.

Anyway, here's my effort, I hope it helps:

#!e:\perl\bin\perl.exe -w use strict; my $data="key1=value1 key2=value2 value3 key4=value4 value5 value6 key +5=value7 key6=value8 key7=value9 value10"; my @values=split(/ /,$data); my ($h,$value, $v, $recentkey); my %output; my @hashkeys; for $value(@values) { if ($value =~ /=/) { ($recentkey, $v) = split(/=/,$value); $output{$recentkey} = $v; } else { $output{$recentkey} .= ",$v"; } } @hashkeys = keys %output; for $h(@hashkeys) { print "$h: $output{$h} \n"; }


Wait! This isn't a Parachute, this is a Backpack!

In reply to Re: Parsing Challenge by gregor42
in thread Parsing Challenge by symŽ

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!
  • 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, details, 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, summary, 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.