storing query result columns data to multiple arrays

From: Kamran (mkazeem_at_isb.paknet.com.pk)
Date: 09/29/04


To: dbi-users@perl.org
Date: Wed, 29 Sep 2004 14:22:26 +0600

Hello all,

I have a table say students with fields (id int, name char(10) ) .

I have data like:-

ID NAME
== ==========
1 Kamran
2 Imran
3 Amir
4 Abid

I want to store the result of the query "directly" in mutiple arrays,
say @ids and @names .

The traditional method will be to fetch each row one at a time and store
that in the arrays like:-

########################## code snip start #########################

my @ids=();
my @names=();

my $SQL = "select * from students";

my $sth = $dbh->prepare($SQL) || die "Cannot prepare SQL
statement\n";

my $howmany = $sth->execute || die "Cannot select from table\n";

while (my @row=$sth->fetchrow_array)
  {
  push (@ids, $row[0]); # $row[0] is ID from table students
  push (@names, $row[1]); # $row[1] is NAME from the table students
  }

######################### code snip end ############################

My question is: "Is there a direct way to put the data coming from
various columns / fields in a table into multiple arrays despite of
running a loop like in the example above?".

I am using Postgresql 7.3 on Redhat Linux 9. I have perl 5. Rather
everything is what comes bundled with Redhat 9. nothing extra.

I don't know if this problem has been addressed in past. If so please
point me in the right direction.

Thanks in advance for your time and help.

Kamran