in reply to Improving if statements
Personally, I don’t find the present way of doing it objectionable. Not at all. It’s obvious what it’s doing. However, you could put the keys into a hash and then loop through it ...
my %stuff = ( moo => "cow", test => "blue", dark => "black", white => "light", house => "home", "all things" => "multi", money => "value", ); while (my ($k, $v) = each(%stuff)) { if { $input =~ /$k/ } { change = $v; last; # break out of the loop now }
But is it “improved?” I probably wouldn’t think so, really, because an if..elsif..else cascade is both extremely readable and easy to change, and you can put the most-common cases right up at the top. Basically, unless you have a compelling reason other than vague thoughts of “efficiency” to change the code as it is now, I’d move on to the next to-do ticket.
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Improving if statements
by choroba (Cardinal) on Sep 18, 2014 at 23:45 UTC | |
by locked_user sundialsvc4 (Abbot) on Sep 19, 2014 at 12:10 UTC | |
by SuicideJunkie (Vicar) on Sep 19, 2014 at 15:15 UTC | |
|
Re^2: Improving if statements
by Anonymous Monk on Sep 19, 2014 at 13:11 UTC |