Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Is this a correct use of map?

by BMaximus (Chaplain)
on Nov 07, 2005 at 23:53 UTC ( [id://506573]=note: print w/replies, xml ) Need Help??


in reply to Re: Is this a correct use of map?
in thread Is this a correct use of map?

I have use warnings; use strict; on and I get no errors or warnings probably because its just a small snippet of a bigger picture that I tried to shrink so I wouldn't bore you with the rest of the code. I'm looking to see if I created the right block to do what I put at the bottom of the code. I guess if it can't be run it really down't help explain what needs to be done. I tried my best to explain. Unfortunately it wasn't good enough.
BMaximus

Replies are listed 'Best First'.
Re^3: Is this a correct use of map?
by GrandFather (Saint) on Nov 08, 2005 at 00:09 UTC

    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.


    Perl is Huffman encoded by design.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://506573]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-03-28 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found