Comments below....

----- Original Message -----
From: "Kenny" <[EMAIL PROTECTED]>
To: "David Ferrance" <[EMAIL PROTECTED]>
Sent: Tuesday, September 04, 2001 6:57 PM
Subject: RE: [htmltmpl] helper module: grouper


> > Now here is where I used to have to write a while loop to build
> > the data structure needed by H:T.  I won't recreate that here.
> >
> > Instead I write:
> >
> > while (@row=$sth->fetchrow_array) { grouper->add_row(@row); }
> >
> > then grouper creates the array of hash refs, containing array
> > refs with hash refs that H:T requires.
>
> ok, you are still using a while() loop, but I think I understand what
you're
> getting at. You're saying that it breaks up the data into several loops
for
> you, based on the 'genre' column in your example. Is that right?

Yes.
>
> I doubt I would use module, I've never needed to group data by more than
one
> column, and for that, I usualy do something like:
>
> -------------
> $list_sth = $dbh->prepare("select distinct type from ..." );
> $list_sth->execute() ;
>
> $sth = $dbh->prepare("select ... from ... where type=?") ;
>
> while( $type = $list_sth->fetchrow_array() ) {
>    $data{$type} = $sth->fetchall_arrayref( {} ) ;
> }

That works, but would probably be a bad choice on a large table, both
because of the overhead in the distinct query and because of the overhead in
running the 2nd query for each distinct value.  I'd benchmark it to be sure
of course, maybe indexes would prevent distinct from table scanning?.  Also
it doesn't scale to more than one level.

Wait, looking at it again it produces a hash of hash refs.  Can H:T handle
this?

> -------------
>
> > This saves you some work for the simple example above, but I
> > actually have cases where I need to do that 3 or 4 levels deep.
> > Rather than write the code over and over (and screw it up most
> > of the time), I've put the code into grouper.
>
> I don't think this would have a wide appeal, it seems pretty specialized.
If
> you do put it into CPAN, I would recommend using column names instead of
> array indexes. I don't know many people who use fetchrow_array() when we
can
> use fetchrow_hashref(). The data comes back in an easily understood data
> structure and with minimal data transmission through the stack (since it
> just passes the reference back, instead of all the data).
>

I just checked, now fetchrow_hashref allows you to force upper or lower
case, which was a problem last time I read the docs so I have been avoiding
it.  It might be useful.  So supporting that would be a good idea.
Unfortunately the docs say you can't count on it not using the same hash ref
every time, so you still need to copy it to its own hash, so the performance
benefit might not be as big as you'd like :(


-Dave



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to