in reply to Re: Given When Syntax
in thread Given When Syntax

Hi Marshall. Thanks for including both an if/else and hash example. I wasn't aware of the more efficient way of using IF and that is what I was looking for (hard to find that stuff by just searching online). Also these make it a lot easier to me to try different solutions. Unfortunately I couldn't get your examples to work. I'm sure I'm overlooking something simple, but when I pass a value to the subroutines I'm not getting any output (see example below for hash). I thought the return would print it to the screen. Please pardon my newbness.

Beyond that, the example I created for the question is rather simplified. In real life I have to deal with a string where I am only concerned with the first number. E.g., n.yy.zzz, where "n" is the number I need to map to. I can get this number simply enough with regex, but I'm not sure how to implement that within your example (can't test because of the output issue). Here's an attempt with an illustration of what I'm trying to do:

use strict; use warnings; #Using a hash table #Evaluate on the first digit of the string { my ($var2) = @_; # $var1 = shift; # is slightly faster with one var # my ($var1, $var2) = @_; # slightly faster than than 2 shifts # normally this minor difference doesn't matter. # Perl can implement the if/else idea very code efficiently. return ("One") if ($var2 =~ /^1/ ); return ("Two") if ($var2 == /^2/ ); return ("Three") if ($var2 == /^3/ ); return undef; } # I want to evaluate the number 1.00.000 # I thought I would just have to pass it # to the subroutine to get a return value. test2(1.00.000);

Once again, many thanks. I appreciate your time.