Oh wise and knowledgeable monks. I seek your wisdom in this hopefully interesting dilemma (ok, enough of that ...). I'm trying to split an array of hashes by a timestamp within the list using map. So far here is what I have.
my @prizes = [ { 'GivenAway' => undef, 'Breakdown' => '0', 'Dcode' => '00000', 'UserId' => '8853', 'Prize' => '500', 'Contracts' => [ { 'Year' => undef, 'Make' => undef, 'Model' => undef, 'Contract' => '1' } ], 'Type' => 'prizes', 'Timestamp' => '2005-11-07 11:18:01', 'WinnerId' => '2026', 'lineid' => '2026', 'Count' => '1', 'Rate' => 'std' }]; my %month = ( '01' => 'January', '02' => 'February', '03' => 'March', '04' => 'April', '05' => 'May', '06' => 'June', '07' => 'July', '08' => 'August', '09' => 'September', '10' => 'October', '11' => 'November', '12' => 'December', ); my @mapped_prizes = map +{ $month{(split(/-| /,$_->{'Timestamp'}))[1]} + => $_ } , @prizes; ## Which hopefully yields 'November' => [{ 'GivenAway' => undef, 'Breakdown' => '0', 'Dcode' => '00000', 'UserId' => '8853', 'Prize' => '500', 'Contracts' => [ { 'Year' => undef, 'Make' => undef, 'Model' => undef, 'Contract' => '1' } ], 'Type' => 'prizes', 'Timestamp' => '2005-11-07 11:18:01', 'WinnerId' => '2026', 'lineid' => '2026', 'Count' => '1', 'Rate' => 'std'}, ];
Sorry about the formatting. I did the best I can with the room I had. Hopefully you get the idea of what I wanted to do. I'm not sure if I should use map +{ } or map +( ). In any case, dumping @mapped_prizes only results in confusion as I'm not sure if its doing what I want it to do. With map +{} Dumper() yields
######MAPPED PRIZES###### $VAR1 = { 'November' => { 'GivenAway' => undef, 'Breakdown' => '0', 'Dcode' => '00000', 'UserId' => '8853', 'Prize' => '500', 'Contracts' => [ { 'Year' => undef, 'Make' => undef, 'Model' => undef, 'Contract' => '1' } ], 'Type' => 'prizes', 'Timestamp' => '2005-11-07 11:18:01', 'WinnerId' => '2026', 'lineid' => '2026', 'Count' => '1', 'Rate' => 'std' } }; $VAR2 = { 'November' => { 'GivenAway' => undef, 'Breakdown' => '0', 'Dcode' => '00000', 'UserId' => '8853', 'Prize' => '300', 'Contracts' => [ { 'Year' => undef, 'Make' => undef, 'Model' => undef, 'Contract' => '100' } ], 'Type' => 'prizes', 'Timestamp' => '2005-11-07 15:06:39', 'WinnerId' => '2027', 'lineid' => '2027', 'Count' => '1', 'Rate' => 'std' } };
And with map +() Dumper() yields:
######MAPPED PRIZES###### $VAR1 = 'November'; $VAR2 = { 'GivenAway' => undef, 'Breakdown' => '0', 'Dcode' => '00000', 'UserId' => '8853', 'Prize' => '500', 'Contracts' => [ { 'Year' => undef, 'Make' => undef, 'Model' => undef, 'Contract' => '1' } ], 'Type' => 'prizes', 'Timestamp' => '2005-11-07 11:18:01', 'WinnerId' => '2026', 'lineid' => '2026', 'Count' => '1', 'Rate' => 'std' }; $VAR3 = 'November'; $VAR4 = { 'GivenAway' => undef, 'Breakdown' => '0', 'Dcode' => '00000', 'UserId' => '8853', 'Prize' => '300', 'Contracts' => [ { 'Year' => undef, 'Make' => undef, 'Model' => undef, 'Contract' => '100' } ], 'Type' => 'prizes', 'Timestamp' => '2005-11-07 15:06:39', 'WinnerId' => '2027', 'lineid' => '2027', 'Count' => '1', 'Rate' => 'std' };
Clever use of map? Or a clever shot in the foot? My thanks, BMaximus
BMaximus

In reply to Is this a correct use of map? by BMaximus

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.