in reply to Re: getting keys of multi-level hashrefs
in thread getting keys of multi-level hashrefs

When dealing with a large hash (the OP stated this was a small example) it may be better to use each rather than getting the overhead of a temporary list using keys. A skelton example:
while (my ($key,$value) = each %$hashref) { print "$key $value\n"; }

Replies are listed 'Best First'.
Re^3: getting keys of multi-level hashrefs
by Neighbour (Friar) on Jul 07, 2010 at 11:24 UTC
    Though I doubt the overhead will have a significant impact, even with really large hashes, using each is indeed more optimized.
    That, and another optimization I thought of after posting my code, would change the subroutine to:
    sub DigThroughHashref { my $hr_data = shift; while (my ($key, $value) = each %{$hr_data}) { if (ref ($value) eq "HASH") { print ("Digging through [$key]:\n"); DigThroughHashref($value, @_, $key); } else { print (join ('->', @_) . "->$key - $value\n"); } } ## end while (my ($key, $value) ... } ## end sub DigThroughHashref
    All occurrances of $_ are now replaced by $key, and all occurrances of $hr_data->{$_} are now replaced by $value.
    Also, the seperate variable @keystack is now optimized out. Instead of pushing the to-be-parsed key explicitly onto the end of @keystack, it is now simply added to the arguments of the sub (which results in it being added to @_ inside the sub).
      Thank you so much for responding so quickly the first time. I was able to see your post before your first edit, after experimenting a bit it's safe to say I learned alot from that. Thank you also for taking your time to optimize that sub.
        Thanks :)
        Come to think of it, there's another beautification I realised later: Changing
        print (join ('->', @_) . "->$key - $value\n");
        to
        print (join ('->', @_, $key) . " - $value\n");
        Because joining first and then appending the last bit of data manually is ugly :-P