go ahead... be a heretic | |
PerlMonks |
Re^2: Using foreach to process a hashby blazar (Canon) |
on Oct 22, 2006 at 13:30 UTC ( [id://579856]=note: print w/replies, xml ) | Need Help?? |
Concerning your question, jdporter answered it but I felt obligated to contribute. If you're processing a hash of considerable size, obviously you'd prefer to use the most efficient functions possible. In order to accomplish this end, the standard perl library includes a module called Benchmark (please see below for an example): I beg to differ. I've done similar interventions before and I know the subject is controversial, but I don't mind being downvoted. Don't misunderstand me: Benchmark.pm is great and I use it quite often, i.e. whenever I really need it. Indeed had the OP called for 'efficiency', it may have been a prefectly sensible answer. But whenever one calls for 'efficiency' one bell should ring, and often does: the very question is whether efficiency would be relevant at all in the situation under consideration. Sometimes it is, sometimes it's not. Actually in the latter case it often turns out to be yet another case of obsession for premature optimization which, we all know, is the root all evil in programming. Now the point is, I see a risk in pointing a newbie like iridius towards these issues: precisely the risk of generating or contributing to that obsession for premature optimization... OTOH your code has a bug:
Indeed you forgot to change $hash{$key} to $monks{$key} in code lifted from previous example, I suppose. In this case it doesn't alter excessively the results, but in others, a similar error may do, and in relevant manners. So I felt obligated to contribute too, but to the effect of reminding the OP that as the above clearly shows, just inserting
at the top of quite about all of his scripts is probably the best way to avoid many common programming mistakes. It just tells perl to give one all the help it can to avoid them. One last piece of advice I can give him, also inspired by this example, is to use descriptive names for his variables: e.g. $monk instead of $key and $rank instead of $value. The rationale being that if it doesn't make sense when mentally translated into English, then chances are it may be wrong...
In Section
Seekers of Perl Wisdom
|
|