Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

How to tweak an AoH?

by neniro (Priest)
on Nov 17, 2004 at 18:05 UTC ( [id://408502]=perlquestion: print w/replies, xml ) Need Help??

neniro has asked for the wisdom of the Perl Monks concerning the following question:

Arrays of Hashes are very popular to Perl-Programmers, especially to those who use HTML::Template. If you populate an AoH using a database query, you'll receive a bunch of hashes that contains always the same keys. That's somehow odd. Is there a way to handle each row like an hash without adding all those unnecessary keys to them?

neniro

Replies are listed 'Best First'.
Re: How to tweak an AoH?
by davido (Cardinal) on Nov 17, 2004 at 18:15 UTC

    It doesn't matter. Hash keys are stored internally only once, even if they are used across multiple hashes. So though it may seem wasteful to have two hashes with the same key in them, it's not. The same goes for multiple rows of anonymous hashes with the same keys. This is an internal, automatic, and invisible optimization, but a smart one at that.

    Update: For the record and posterity, I wanted to mention that this optimization is known as "Shared strings", and is discussed in Advanced Perl Programming (the Cougar book), published by O'Reilly & Associates. Find it in Chapter 20: Perl Internals, Section 3.3.1. The exact quote is:

    To prevent unnecessary duplication, the actual key strings are maintained in a systemwide shared string table (strtab in strtab.h). strtab is a simplified HV: each value here keeps a reference count of the number of uses of that string.

    It's also discussed in http://www.faqs.org/docs/perl5int, where Simon Cozens writes the following in section 4.2.2.2:

    The HEK stored inside a hash entry represents the key: it contains the hash value and the key itself. It's stored separately so that Perl can share identical keys between different hashes - this saves memory and also saves time calcu.llating the hash value.

    Neither of those sources are as ubiquitous as a POD reference, but aside from unraveling the source, they seem to be the best insight we've got on the topic.


    Dave

      Thats what I was asking for. No need to do these optimization myself if perl already does it itself.

      Thanks in advance,
      neniro

Re: How to tweak an AoH?
by ikegami (Patriarch) on Nov 17, 2004 at 18:13 UTC
    How are keys unecessary to a hash??? Maybe you want something like:
    ... $sth->execute(...) or die(...); my %field_idx = do { my $i = 0; map { $_ => $i++ } @{$sth->{NAMES}} }; my $row; while ($row = $sth->fetch()) { print($row->[$field_idx{'userid' }], $/); print($row->[$field_idx{'password'}], $/); } ...
      How are keys unecessary to a hash???

      Of course they are necessary to a normal hash - but for an AoH it isn't necessary to have complete hashes cause the keys for each row are always the same.

Re: How to tweak an AoH?
by jZed (Prior) on Nov 17, 2004 at 18:19 UTC
    I'm not exactly sure what you're asking about, perhaps you could give an example. If what you want is just the values without the keys, that would be an AoA and is obtainable from the database with selectall_arrayref(). If you want each row keyed to a given column, that is an HoH, obtainable with selectall_hashref(). If you want an array of hashrefs for each row, that is an AoH and obtainable with selectall_arrayref(...Slice=>{}). If you want to be able to refer to the results of a database fetch by name without populating a hash, you can predefine a set of variables and populate them with bind_cols. But perhaps you're asking about something else?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-20 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found