BMaximus has asked for the wisdom of the Perl Monks concerning the following question:
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() yieldsmy @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'}, ];
And 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' } };
Clever use of map? Or a clever shot in the foot? My thanks, BMaximus######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' };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Is this a correct use of map?
by Aristotle (Chancellor) on Nov 08, 2005 at 00:04 UTC | |
|
Re: Is this a correct use of map?
by GrandFather (Saint) on Nov 07, 2005 at 23:41 UTC | |
by BMaximus (Chaplain) on Nov 07, 2005 at 23:53 UTC | |
by GrandFather (Saint) on Nov 08, 2005 at 00:09 UTC |