in reply to A quesion about referencing lists
A while back i broke down and wrote a sub that transforms database results into an HTML::Template-friendly data structure:# assumption: @words == @numbers use Data::Dumper; my @words = qw(I Am Cool); my @numbers = qw(1 2 3); my @loop_data = map { { WORD => $words[$_], NUMBER => $numbers[$_], } } 0..$#words; print Dumper \@loop_data;
I probably should have fetched a hash, but i didn't. Anyway, you can call it like so:sub db2tmpl { my ($sth,@bind) = @_; $sth->execute(@bind); my $fields = $sth->{NAME}; my $rows = $sth->fetchall_arrayref; my @loh; for my $row (@$rows) { my %hash; @hash{@$fields} = @$row; push @loh, {%hash}; } return \@loh; }
I am sure that i saw a CPAN module that did something very similar. I can't seem to find it now (slipped away), so if anyone knows what i am talking about ... please tell me. :) Hope this helps.my $sth = $dbh->prepare('select * from student'); my $students = db2tmpl($sth);
UPDATE:
gmax reminded me that this is overkill ... just call
$sth->fetchall_arrayref({}) (notice the
anonymous hash ref as the arg).
jeffa
L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR B--B--B--B--B--B--B--B-- H---H---H---H---H---H--- (the triplet paradiddle with high-hat)
|
|---|