If I'm right, you've got some string which parses out into an ID, then a series of fields. You want to create a hash with key of ID and value being some hashref, pointing to a hash with key of name/email/etc and value being the value.

The basic code for this would be something like:

($ID,$name,$email,$title,$date,...) = /(ID)(name)(email)(title)(date)( +...)/; $hash{$ID}{name} = $name; $hash{$ID}{email} = $email; ...

Now, what you're doing is, essentially, assigning a hash slice. Now, if you don't know, a hash slice is done something like:

my @keys = ('a', 'b', 'c'); my @vals = (1, 2, 3); my %hash; @hash{@keys} = @vals;

At this point, $hash{a} == 1, etc.

For a hash of hashes, I think the syntax is something like:

my @keys = ('a', 'b', 'c'); my @vals = (1, 2, 3); my %hash; @($hash{key}}{@keys} = @vals;

But, please test this out.

To marry this with the code you have right now, I'd do something like:

my @keys = ('name', 'email', 'title', ...); ($ID, @vals) = /(ID)(name)(email)(title)(date)(...)/; @{$hash{$ID}}{@keys} = @vals;

In reply to Re: Hash asignement with RE and map() by dragonchild
in thread Hash asignement with RE and map() by bobione

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.