Steny has asked for the wisdom of the Perl Monks concerning the following question:

I'm using the following code to select rows from a table, modify the contents (formatting a number) then I want to send it to HTML::Template to use in a LOOP and print out in a table... my problem is that, once I modify the data, I don't know how to get the hash into an array so that it will work in HTML::Template.

Current code:
my $statement = "SELECT name, number, nw, land, strat, last_up +dated, ownerID FROM someTable ORDER BY numbe +r ASC"; $sth = $dbh->prepare($statement); + $sth->execute() or die "Can't execute the query: $sth->errstr" +; my $ref = $sth->fetchall_hashref('number'); for my $key( %$ref ) { $ref->{$key}->{nw} = addComma( $ref->{$key}->{nw} ); } my $countries = $sth->rows(); $sth->finish;
What I use to do was do something like this:
my $rows = $dbh->selectall_arrayref(".....");
then simply send $rows to the Template. But because I have to modify the data, I don't know how to get it back into a format that the template will accept.

P.S. "number" is the key field in the table. Is that what I should be using how I am, in this line:
my $ref = $sth->fetchall_hashref('number');
?
Could someone please enlighten me?

Thank you so much!


Steny

edit (broquaint): added <code> tags

Replies are listed 'Best First'.
Re: Sending hash to HTML::Template
by blokhead (Monsignor) on May 17, 2004 at 06:08 UTC
    Use fetchall_arrayref (instead of fetchall_hashref) to get an array of hashes right from the start: (untested)
    my $rows = $sth->fetchall_arrayref({}); $_->{nw} = addComma( $_->{nw} ) for @$rows; $tmpl->param( items => $rows ); ## Suitable for: ## ## <tmpl_loop items> ## <tmpl_var name> ## <tmpl_var nw> ## <tmpl_var number> ## </tmpl_loop items>
    You can also combine the prepare/execute/fetchall_arrayref into a single call to selectall_arrayref, if you use the Slice parameter. This is the simplest way to send query results directly into HTML::Template. Take a look at the excellent DBI recipes for more info.

    blokhead