in reply to How to get an acceptable H::T AoH ref

  1. You have param( showlist => $allshows ), which is correct, on its face.
  2. $allshows, however, is an AoAoH, not the AoH you might think. (Use Data::Dumper if you don't believe me.)
  3. You really want
    push @$allshows, @{ $sth->fetchall_arrayref( {} ) };
  4. Of course, this assumes you want everything displayed in one TMPL_LOOP.
  5. If you want a loop of loops, you need something more akin to
    push @$allshows, { inner => $sth->fetchall_arrayref( {} ) };
    which would create an AoHoAoH (H::T doesn't scale well. Template Toolkit is much better for this, but it has other issues in the simple case.)

------
We are the carpenters and bricklayers of the Information Age.

Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

I shouldn't have to say this, but any code, unless otherwise stated, is untested

Replies are listed 'Best First'.
Re^2: How to get an acceptable H::T AoH ref
by bradcathey (Prior) on Sep 11, 2004 at 01:01 UTC

    Okay, I'm baffled. When I go back and look at an AoH that *does* works with an H::T loop, and Dump it, I get something like:

    my $allshows = $sth->fetchall_arrayref({}) }; print Dumper($allshows); $VAR1 = [{ 'title' => 'All Bets Are Off', 'type' => '2', 'number' => '215', 'id' => '1' }, { 'title' => 'Who Is My Neighbor', 'type' => '2', 'number' => '217', 'id' => '2' }];

    Which matches up with the H::T documentation. When I go back to my original code:

    push @$allshows, $sth->fetchall_arrayref({}); ... print Dumper(@$allshows); $VAR1 = [{ 'title' => 'All Bets Are Off', 'type' => '2', 'number' => '215', 'id' => '1' }]; $VAR2 = [{ 'title' => 'Who Is My Neighbor', 'type' => '2', 'number' => '217', 'id' => '2' }];

    But when I try dragonchild's I get:

    ... push @$allshows, @{ $sth->fetchall_arrayref({}) }; ... print Dumper(@$allshows); $VAR1 = { 'title' => 'All Bets Are Off', 'type' => '2', 'number' => '215', 'id' => '1' }; $VAR2 = { 'title' => 'Who Is My Neighbor', 'type' => '2', 'number' => '217', 'id' => '2' };

    Seems like I just need to append the single element of the array with hashes. With this I get no error, but no data either:

    ... $allshows[0] .= $sth->fetchall_arrayref({}); ... print Dumper(\@allshows); $VAR1 = [ 'ARRAY(0x83db738)ARRAY(0x843ef94)' ];

    It seems I'm close, but no cigar. What am I missing?


    —Brad
    "Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton
      You're misusing Data::Dumper, which is what's confusing you. D::D takes a list of references to data structures you want to dump out. That's why you're seeing the $VAR1, $VAR2, etc. If you're trying to print out just one thing, you should only see $VAR1. Try the following with my code.
      print Dumper( $allshows );
      You'll get something along the lines of:
      $VAR1 = [{ 'title' => 'All Bets Are Off', 'type' => '2', 'number' => '215', 'id' => '1' }, { 'title' => 'Who Is My Neighbor', 'type' => '2', 'number' => '217', 'id' => '2' } ];
      which is what you want to pass to H::T - an AoH.

      The reason why your last try isn't working is because you're doing string concatenation. The result of assigning an array reference to a string is the ARRAY(0x83db738) crap, which is the type and the memory address of the reference. That isn't what you're looking for, I suspect.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

      I shouldn't have to say this, but any code, unless otherwise stated, is untested

        Okay, you are right dragonchild. Thanks for your solution—I would have never figured it out on my own.

        However, nothing worked until I added a slash* in front of @$, so:

        params(showlist => \@$allshows);

        *thanks to the HTML::Template mail serve


        —Brad
        "Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton