One thing you could do to accellerate your code by probably an order of magnitude right away is to change %{$tableData{$hashkey}{$hashkey2}} = %tblData; to $tableData{$hashkey}{$hashkey2} = \%tblData;

The syntax in the first sample causes a full copy of the hash to be created from scratch; the other way simply stores a reference to the already built hash, which is much faster. Since a new hash is created by my on each loop iteration, taking a reference to it is okay.

You can further simplify this by reading perldoc -f delete and finding out that the function returns the value of the deleted element (so long as it's not a tied hash anyway). This means

my $hashkey2 = $tblData{$key2}; delete $tblData{$key2}; $tableData{$hashkey}{$hashkey2} = \%tblData;
can be contracted to $tableData{$hashkey}{delete $tblData{$key2}} = \%tblData;

Now on to the juicy stuff. You can see that adding another level of depth to the hash assignment code always follows the same scheme, right? Well, it's just a matter of teaching Perl how to do that job for us. What we do is use eval to build a closure.

my @key = grep defined, $key, $key2, $key3; my %tableData; my $store_row = eval join("", q( sub { my $row = shift; $tableData), map("{delete \$row->{'$_'}}", @key), q( = $row; })); while (my @data = $dbh -> dbnextrow) { s/^\s|\s$//g for @data; my %tblData; @tblData{@columnnames} = @data; $store_row(\%tblData); }

Turns out to be rather short and painless. (Note I haven't tested this; I don't see any mistakes though.) This will work for any depth you want; just add keys to the @key array and off you go. I'm fairly confident that performance will be excellent. It would be best if you can arrange your keys to arrive in a @key array right away rather than in a loose collection of scalars. Note that the closure built takes a hashref as parameter.

Pay attention to the backslash-escaped sigil in "{delete \$row->{$_}}"; while the $_ is not protected. That means the keynames get interpolated at "compile time", as the closure's code is built.

Update 2002-09-09: "{delete \$row->{$_}}" will malfunction if the keyname has any whitespace characters. Added single quotes.

Makeshifts last the longest.


In reply to Re: Recursive hash assignment by Aristotle
in thread Recursive hash assignment by Tanalis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.