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). | [reply] [d/l] |
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.
| [reply] |
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
| [reply] [d/l] [select] |