This isn't necessarily the best advice, but I personally find these traversal problems really fun to solve in perl using the $_ variable. It also gives the fastest execution time.
sub recursive_mult_by_100() {
if (!ref) { $_ *= 100 }
elsif (ref eq 'ARRAY') { recursive_mult_by_100 for @$_ }
elsif (ref eq 'HASH') { recursive_mult_by_100 for values %$_ }
else { die "Unhandled data type ".ref; }
}
recursive_mult_by_100 for $deep;
This takes advantage of the feature where Perl aliases things to $_ during a for loop, so modifying $_ modifies the underlying thing, and where ref operates on $_ by default if you don't give it a regular parameter.
I say it's not the best advice because it isn't the most readable code, nor a standard pattern that people expect. Note that the () on the function stops it from accepting parameters, which helps you avoid the mistake of calling recursive_mult_by_100($data) and getting buggy results.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.