What I have working at the moment is a hash that is setup with two embedded map{} statements. This setup allows you to declare multiple hash keys to have the same value in a eye-pleasing manner. Now what I need to accomplish is one step further: populating this hash from an array with elements that will be splitted to extract the data to place into the hash.

Here's the code I have so far (working code):

my %hash = map { my $item = pop @$_; map { $_, $item } @$_ } [ ( 'key1', 'key2', 'key3' ) => 'Value for key1, key2, and key3' ], [ qw( key4 key5 key6 ) => 'Value for key4, key5, key6' ], [ ( 'key7', 'key8', 'key9' ) => 'Value for key7, key8, key9' ]; # So we can now do the following. Hopefully everyone # now understands what the hash declaration above does # Output shown after __END__ while ( my($k,$v) = each %hash ) { print "$k: $v\n"; } __END__ key1: Value for key1, key2, key3 key2: Value for key1, key2, key3 key3: Value for key1, key2, key3 key4: Value for key4, key5, key6 key5: Value for key4, key5, key6 etc etc etc

Okay perfect! I can now set multiple hash keys to contain the same value. This is precisely what I needed and I got that working. Now, the tougher part that I simply cannot grasp onto? How to fill that same hash, populating it from an array. The ideal array for my case here would be the key names separated by a tilde ('~'), and the value for those keys to be at the end of the element, separated from the key names by a newline ('\n'). I could live with there needing to be a tilde before the newline. So the array for the above example would look exactly like this:

my @data = ( qq{key1~key2~key3 Value for key1, key2, key3}, qq{key4~key5~key6 Value for key4, key5, key6}, qq{key7~key8~key9 Value for key7, key8, key9} );

Any suggestions as to how one would take the above array and create the hash I posted even further above? I know exactly how I want everything, now if I only had the brain power to figure it all out :)


      C:\>shutdown -s
      >> Could not shut down computer:
      >> Microsoft is logged in remotely.
    

update (broquaint): title change (was Split array to populate a hash with multiple keys pointing to same value)


In reply to Splitting an array to populate hash by Coruscate

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.