naChoZ has asked for the wisdom of the Perl Monks concerning the following question:
Ok, in a nutshell, I'm trying to convert this:
(which works exactly as I want)my $hash_ref; while ( my $row = $sth->fetchrow_hashref ) { push @{ $hash_ref->{ $row->{ 'msgid' } } }, { 'option' => $row->{ 'option' }, 'option_data' => $row->{ 'option_data' } }; }
To this:
(which doesn't work, but iterates everything in < 10 seconds as opposed to the other way which is around 150 seconds to iterate all the rows.)my $rows = $sth->fetchall_arrayref; my %hash = map { $_->[0] => { 'option' => $_->[1], 'option_data' => $_->[2] }; } @{$rows};
| id | option | option_data |
| 1234 | foo | fooval |
| 1235 | bar | barval |
| 1236 | baz | bazval |
| 1234 | quux | quuxval |
So, like the results of the while loop, I'd like the map to end up with this:
'1234' => [ { 'option_data' => 'fooval', 'option' => 'foo' }, { 'option_data' => 'quuxval', 'option' => 'quux' } ], '1235' => [ { 'option_data' => 'barval', 'option' => 'bar' } ], '1236' => [ { 'option_data' => 'bazval', 'option' => 'baz' } ]
--
"A long habit of not thinking a thing wrong, gives it a superficial appearance of being right." -- Thomas Paine
naChoZ
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: while/push to map, data structure confusion
by dragonchild (Archbishop) on Jul 30, 2004 at 20:22 UTC | |
|
Re: while/push to map, data structure confusion
by Roy Johnson (Monsignor) on Jul 30, 2004 at 21:26 UTC | |
|
Re: while/push to map, data structure confusion
by diotalevi (Canon) on Jul 30, 2004 at 21:28 UTC | |
|
Re: while/push to map, data structure confusion
by JamesNC (Chaplain) on Jul 31, 2004 at 23:39 UTC | |
|
Re: while/push to map, data structure confusion
by bageler (Hermit) on Jul 31, 2004 at 19:54 UTC | |
|
Re: while/push to map, data structure confusion
by naChoZ (Curate) on Aug 01, 2004 at 12:45 UTC |