Perl Monk, Perl Meditation | |
PerlMonks |
Re^3: Hash Manipulationby hippo (Bishop) |
on Jun 08, 2021 at 15:19 UTC ( [id://11133664]=note: print w/replies, xml ) | Need Help?? |
In your code you have this block (I've shortened the print statements for clarity):
This is confusing because of the empty block straddling the first 2 lines. There are a couple of ways to avoid it in general. One is to negate the condition:
The other is to turn the if into an unless:
Either of these removes the empty block, so that's a plus. But we can go farther. The logic can be simplified because really you have only 2 conditions as indicated by my rewritten print statements. So we can remove the outer condition by folding it into the else like so:
Finally, deducing from your previous code that $ele is only ever a whole number you can switch to using numerical comparisons rather than string comparisons (== instead of eq, etc.). Perhaps this is the clearest:
I understand that you are just hacking about with this code but wanted to point out these alternative ways of going about the logical if-blocks. The simpler you can make it the fewer bugs can creep in and that has to be a good thing. 🦛
In Section
Seekers of Perl Wisdom
|
|