You might find Data::Dumper useful for examining what data structure you have. Use fetchrow_arrayref() or fetchrow_hashref() (as others have already suggested) so that you only need to address one row at a time.
Read the DBI documentation thoroughly. It's lengthy but it'll save you debugging time, especially if you are new to Perl. Copy the examples carefully; Perl is very intolerant to typos. And remember error checking, even though I didn't here.
#!/usr/bin/perl use strict; use warnings; use DBI; use Data::Dumper; my $Sort = 'bydate'; my %DB; my $dbh = DBI->connect('DBI:Pg:dbname=tilrman'); my $sth = $dbh->prepare('SELECT name,height,width,date FROM foo'); $sth->execute(); while (my $row = $sth->fetchrow_arrayref()) { if ($Sort eq 'byname') { $DB{$row->[0]} = [ @{$row}[1, 2, 3] ]; } elsif ($Sort eq 'bydate') { $DB{$row->[3]} = [ @{$row}[1, 2, 0] ]; } } $sth->finish(); $dbh->disconnect(); print Data::Dumper->Dump([\%DB], ['*DB']);
In reply to Re: DBI help, aka confused newbie
by TilRMan
in thread DBI help, aka confused newbie
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |