Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Hello, I have a query which returns a set of rows. Each row has an id field:
$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' } ];
Now, I want to re-index this flat set of rows by that id field as follows:
[ $id1 => [ { $hashrefs_with_id1 } ... ] $id2 => [ { $hashrefs_with_id2 } ... ] ]
I tried the map_hashes method of my favorite DBI wrapper, DBIx::Simple, but it only returns one hashref for each mapping key:
$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' } };
and I want an arrayref of them all. It's easy enough for me to write something to do this:
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);
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.



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"


In reply to Re-indexing a SQL resultset by id by metaperl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (12)
    As of 2015-07-02 21:53 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (45 votes), past polls