Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: An Array Without and Index

by Neighbour (Friar)
on Aug 13, 2012 at 09:08 UTC ( #987077=note: print w/replies, xml ) Need Help??


in reply to An Array Without and Index

If your u2addr-table isn't extraordinary large, you could just fetch the whole thing in a hashref and loop through that:
my @results; my $hr_addresses = $dbh->selectall_hashref("SELECT addr, id FROM u2add +r", "addr"); if (!defined $hr_addresses) { die("Error executing query: " . $dbh->er +rstr); } foreach my $addr (@addr) { if (defined $hr_addresses->{$addr}) { push(@results, $hr_addresses->{$addr}) } else { print("Error, ID of address [$addr] not found\n"); } }
The use of selectall_hashref (or selectall_arrayref) relieves you of having to prepare, execute and fetch* everything yourself. All that's left is to use the results (in this case as an in-memory hashed lookup for all addresses in @addr). On top of that, calling finish isn't neccesary when you are fetching *all* the results, only when you want to abort fetching results partly through.
Besides that, if selectall_hashref (or selectall_arrayref) returns undef, then executing the query went wrong. This is a very nice thing to know. This is also different from the query executing correctly, but returning no results (which is slightly more difficult to check when using fetchrow_array).

Edit: Whoops, got the keyfield of the query wrong, fixed now.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://987077]
help
Chatterbox?
[stevieb]: It seems as though some Strawberry Perl downloads have incorrect checksums all of a sudden. Could someone please download http://strawberryp erl.com/download/5 .12.3.0/strawberry -perl-5.12.3.0- portable.zip and do an sha1sum on...
[stevieb]: ...it? I'm getting 309ad7a9ba74614fcd 0c65bff7ea4400c10f a92f, but the http://strawberryp erl.com/releases. html states 0e267fc2cf5a16126d a6f9520cc7664db63d 2b57 and want to ensure it's not just me
[stevieb]: ...or the proper download link and release page even...
[pryrt]: da39a3ee5e6b4b0d32 55bfef95601890afd8 0709 *strawberry-perl-5 .12.3.0-portable. zip
[pryrt]: matches neither. :-)
[stevieb]: wtf!? lol
[stevieb]: I'm checking some other versions. This is a little frightening

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2017-03-29 20:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (353 votes). Check out past polls.