in reply to Re^2: Is this a correct use of map?
in thread Is this a correct use of map?
Try altering the following code to show your problem if it doesn't do what you are trying to achieve already:
use strict; use warnings; use Data::Dumper; my @prizes = ( { 'Timestamp' => '2005-11-07 11:18:01', 'WinnerId' => '2026', 'lineid' => '2026', }, { 'Timestamp' => '2005-01-07 11:18:01', 'WinnerId' => '2001', 'lineid' => '2001', }, { 'Timestamp' => '2005-05-07 11:18:01', 'WinnerId' => '2015', 'lineid' => '2015', }, ); 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; print Dumper (\@mapped_prizes);
Prints:
$VAR1 = [ [ 'November', { 'Timestamp' => '2005-11-07 11:18:01', 'lineid' => '2026', 'WinnerId' => '2026' } ], [ 'January', { 'Timestamp' => '2005-01-07 11:18:01', 'lineid' => '2001', 'WinnerId' => '2001' } ], [ 'May', { 'Timestamp' => '2005-05-07 11:18:01', 'lineid' => '2015', 'WinnerId' => '2015' } ] ];
Note that I removed some of the fields from the hash - only a couple are needed to demonstrate the issue. Note also that I added a couple of entries because it seems that more than one is required to show the issue. And further, note that I reduced the reference nesting by a level in the @prizes array which cleans up the warnings that I was seeing and also that I changed the +{}, in your original map (that looks rather peculiar) to a more usual {} and altered the contents of the map expression to do what I guess you want to do: generate an list of pairs of month and prize elements. I don't see why you don't want this as a hash however.
|
|---|