Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: push/append to hash in a loop

by agianni (Hermit)
on Mar 21, 2007 at 00:58 UTC ( [id://605772]=note: print w/replies, xml ) Need Help??


in reply to push/append to hash in a loop

Consider using fetchrow_hashref instead of fetchrow_arrayref:

while ( my $row = $sth->fetchrown_hashref ){ $hasho{ $row{org_id} } = $row{org_nm}; }

If you really need to use fetchrow_array for some reason, you can just dump the results directly into variables:

while ( my ( $org_id, $org_nm ) = $sth->fetchrow_array()) { $hasho{$org_id} = $org_nm; }

Or, if you want to get real Perl-ish:

my %hash = map { $_->{org_id} => $_->{org_nm} } @{ $sth->fetchall_arra +yref };

Also, no need for the quoting of variables on the last line of your code.

split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(join(q{},map +{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79*36997,13 +*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));

Replies are listed 'Best First'.
Re^2: push/append to hash in a loop
by grinder (Bishop) on Mar 21, 2007 at 07:56 UTC
    If you really need to use fetchrow_array for some reason

    A good reason is that it's faster (update: erm: fetchrow_arrayref I mean. fetchrow_array is a fetch with training wheels). In my experience, if I want to do something hashy with the results of a DB fetch, it's usually faster to do exactly what I want in a loop over fetchrow_arrayref, rather than fetchrow_hashref.

    That is, you don't always need the entire row stored as key/value pairs. Usually all you need are a couple of key/value pairs (that usually correspond to a unique index tuple) and the rest of the values you can store in a list.

    And that list of values winds up being formatted in a simple join or join( '...', map). The cost of uselessly hashifying these values is what hurts.

    • another intruder with the mooring in the heart of the Perl

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2024-04-19 22:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found