Re: Parsing Results from fetchall_hashref()



On 2007-10-26 19:39:38 -0000, Rob Wilkerson wrote:
I'm trying to debug a problem in a Perl script that retrieves records
from an Oracle database and writes those records to a flat file. I'm
retrieving the records using fetchall_hashref() and have found that at
least one record isn't being written correctly so I'm trying to figure
out why.

I can access the value of each member just fine using $mediaref-
{$media_id}->{'ADID'} and I can access the size of the entire record
set as keys ( %$mediaref ). What I need to know, though, is how many
elements exist in $mediaref->{$media_id}, but I can't figure out
whether that's possible or how to do it. I've tried all kinds of
variations of keys ( %$mediaref->{$mediaid} ) with no luck at all.

scalar keys %{ $mediaref->{$mediaid} }

But I'm quite sure that will return the same number for all rows in the
query. Maybe you are more interested in the number of fields which are
not null:

scalar grep {
defined $mediaref->{$mediaid}{$_}
} keys %{ $mediaref->{$mediaid} }

Is it possible?

Yes.

Am I correctly guessing at how the system stores fields (i.e. as a nested
hash)?

Yes. No need to guess, though. perldoc DBI states that quite clearly:

The "fetchall_hashref" method can be used to fetch all the data to be
returned from a prepared and executed statement handle. It returns a
reference to a hash containing a key for each distinct value of the
$key_field column that was fetched. For each key the corresponding
value is a reference to a hash containing all the selected columns and
their values, as returned by fetchrow_hashref().

hp

--
_ | Peter J. Holzer | If I wanted to be "academically correct",
|_|_) | Sysadmin WSR | I'd be programming in Java.
| | | hjp@xxxxxxxxx | I don't, and I'm not.
__/ | http://www.hjp.at/ | -- Jesse Erlbaum on dbi-users

Attachment: pgpBAELOnCO7Z.pgp
Description: PGP signature



Relevant Pages

  • Re: Suggestion for an AES Based Hash Function
    ... amateurs posting here seem to favour cyphers, I go for hash functions. ... Rather than trying to use a strong one-way compression function would ... I have used a fixed set of keys in two places. ... preferred to have the first round keys fixed and to start deriving ...
    (sci.crypt)
  • Suggestion for an AES Based Hash Function
    ... amateurs posting here seem to favour cyphers, I go for hash functions. ... Rather than trying to use a strong one-way compression function would ... The round function ... I have used a fixed set of keys in two places. ...
    (sci.crypt)
  • Re: Hashes of Hashes
    ... group = STRING ... If I swap the order of the declarations, then all of the keys for the ... hash that is referenced by $Data. ... This is known as a "symbolic reference" - using the ...
    (perl.beginners)
  • Re: Reference question
    ... Paul Lalli wrote: ... >>> If you want to get a reference without the extraneous temporary ... >> values are refs to another hash, whose keys are strings and values are ...
    (perl.beginners)
  • Re: sort unique
    ... given that a hash table is not ... IMO if the vendor's algorithm does something "obvious", ... function to eliminate keys that hash to the same bucket per some ... strings of random lengths, and two strings are ...
    (comp.lang.lisp)