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

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).

Replies are listed 'Best First'.
Re^4: getting keys of multi-level hashrefs
by mask_man (Initiate) on Jul 07, 2010 at 20:12 UTC
    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