Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: From database to hash table

by Tux (Canon)
on Sep 06, 2012 at 17:26 UTC ( [id://992155]=note: print w/replies, xml ) Need Help??


in reply to Re: From database to hash table
in thread From database to hash table

Faster alternative 1:

my %hash; my %rec; my $sth = $db->prepare ("select * from tablename"); $sth->execute; $sth->bind_columns (\@hash{@{$sth->{NAME_lc}}}); while ($sth->fetch) { $hash{$rec{week}} = [ $rec{month}, $rec{workperiod} ]; }

Faster alternative 2:

my %hash; my $sth = $db->prepare ("select week, month, workperiod from tablename +"); $sth->execute; $sth->bind_columns (\my ($week, $month, $workperiod)); while ($sth->fetch) { $hash{$week} = [ $month, $workperiod ]; }

Readabler alternative 3:

my $dbh = DBI->connect ("dbi:$driver:", "user", "pass", { FetchHashKey +Name => "NAME_lc" }); my %hash; my $sth = $db->prepare ("select week, month, workperiod from tablename +"); $sth->execute; while (my $rec = $sth->fetchrow_hashref) { $hash{$rec->{week}} = [ $rec->{month}, $rec->{workperiod} ]; }

Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://992155]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2024-04-25 09:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found