.------[ Scott Taylor wrote (2002/09/11 at 11:07:21) ]------
|
| Hello,
|
| I have an SQL table with 2 fields, ID and Descr and I want to load these
| into an array and a hash.
|
| First the array @SecID should be just the first field and then the hash
| %Sections should be ID=>Descr
|
| All I get is garbage
|
| This is the code that doesn't work, I know I'm missing something here, but
| not what:
|
| $SQL = qq[Select * from T_Sec_ID];
| $sth = $dbh->prepare($SQL);
| $sth->execute;
|
| my %Sections = $sth->fetchrow_hashref;
| $sth->finish;
|
| $SQL = qq[select secid from T_Sec_ID];
| $sth = $dbh->prepare($SQL);
| $sth->execute;
|
| my @SecIDs = $sth->fetchrow_arrayref;
| $sth->finish;
|
| Here is the output, not what I want:
| HASH(0x82c6388)
| ARRAY(0x82c6370)
|
`-------------------------------------------------
There are two ways to do this the more advanced and the easy:
Easy way:
my %Sections;
my $sth = $dbh->prepare("SELECT ID, Descr FROM T_Sec_ID");
$sth->execute;
while( my($id, $desc) = $sth->fetchrow ) {
$Sections{$id} = $desc;
}
$sth->finish;
my @SecIDs = keys(%Sections);
You don't really need the Array as you can always call
keys(%Sections) and it will return the array you were trying to
build into @SecIDs. This will also remove the need to do two SQL
calls which isn't very efficient.
Advanced way using references:
my $section_ref;
my $sth = $dbh->prepare("SELECT ID, Descr FROM T_Sec_ID");
$sth->execute;
$section_ref = $sth->fetchrow_hashref;
my @SecIDS = keys( %{ $section_ref } );
You'll probably want to read up on Perl references in the perlref
man page.
---------------------------------
Frank Wiles <[EMAIL PROTECTED]>
http://frank.wiles.org
---------------------------------
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]