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