metaperl has asked for the wisdom of the Perl Monks concerning the following question:
Hello, I have a query which returns a set of rows. Each row has an id field:
The mantra of every experienced web application developer is the same: thou shalt separate business logic from display. Ironically, almost all template engines allow violation of this separation principle, which is the very impetus for HTML template engine development.
Now, I want to re-index this flat set of rows by that id field as follows:$VAR1 = [ { 'amount' => '58.80', 'quantity' => '1', 'product_listid' => '800000FA-1450218051', 'id' => '3938716373870073', 'customer_listid' => '8000024C-1450196328', 'datetime' => '1309966331' }, { 'amount' => '1.34', 'quantity' => '1', 'product_listid' => '800000FC-1450218057', 'id' => '3938716373870073', 'customer_listid' => '8000024C-1450196328', 'datetime' => '1309966331' }, { 'amount' => '2.14', 'quantity' => '1', 'product_listid' => '8000010C-1450218105', 'id' => '2446438717782054', 'customer_listid' => '80000245-1450196318', 'datetime' => '1310146917' }, { 'amount' => '1.34', 'quantity' => '1', 'product_listid' => '800000FC-1450218057', 'id' => '2446438717782054', 'customer_listid' => '80000245-1450196318', 'datetime' => '1310146917' } ];
I tried the map_hashes method of my favorite DBI wrapper, DBIx::Simple, but it only returns one hashref for each mapping key:[ $id1 => [ { $hashrefs_with_id1 } ... ] $id2 => [ { $hashrefs_with_id2 } ... ] ]
and I want an arrayref of them all. It's easy enough for me to write something to do this:$VAR1 = { '2446438717782054' => { 'amount' => '1.34', 'quantity' => '1', 'product_listid' => '800000FC-145021 +8057', 'customer_listid' => '80000245-14501 +96318', 'datetime' => '1310146917' }, '3938716373870073' => { 'amount' => '1.34', 'quantity' => '1', 'product_listid' => '800000FC-145021 +8057', 'customer_listid' => '8000024C-14501 +96328', 'datetime' => '1309966331' } };
but someone has to have needed this and written it already... maybe there's a data structure utility that does this sort of reworking of an array? UPDATEDBIx::SQLCrosstab does exactly what I want, except that it aggregates the non-keyed columns instead of making an arrayref of hashrefs out of them. For now, I will just code it.my @row = $r->hashes; # produce flat format of arrays of hashrefs my %row; for my $row (@row) { push @{$row{$row->{id}}}, $row; } warn Dumper(\%row);
The mantra of every experienced web application developer is the same: thou shalt separate business logic from display. Ironically, almost all template engines allow violation of this separation principle, which is the very impetus for HTML template engine development.
-- Terence Parr, "Enforcing Strict Model View Separation in Template Engines"
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re-indexing a SQL resultset by id
by Juerd (Abbot) on Jul 09, 2011 at 22:28 UTC | |
by metaperl (Curate) on Jul 09, 2011 at 23:36 UTC | |
by Juerd (Abbot) on Jul 09, 2011 at 23:42 UTC | |
Re: Re-indexing a SQL resultset by id
by sundialsvc4 (Abbot) on Jul 08, 2011 at 22:17 UTC |
Back to
Seekers of Perl Wisdom