Have an idea?

Visit Sawtooth Software Feedback to share your ideas on how we can improve our products.

Using the StringToNumber function and Perl

I just noticed the StringToNumber(String) function within the Sawtooth Software help under SSI Script, but not under the Perl help?

Is this on purpose?

Or should we be using the alternative Perl function ...
# String type 
$string1 = "27"; 
# conversion of string to int  
# using typecasting int() 
$num1 = int($string1); 

asked Jun 25, 2019 by Paul Moon Platinum (101,255 points)

1 Answer

+1 vote
I don't control what ends up in Lighthouse Studio's Help, but I could recommend changes be made to areas in the documentation if users feel it is warranted.

Regarding this particular topic, keep in mind that Perl is a dynamic typed programming language.  What this means is that Perl plays a bit fast and loose with variable typing, letting the programmer perform implicit type conversions.  Here are two example Perl scripts you can try out that demonstrate this:

my $a = 1;
my $b = '2';
return $a + $b; // 3

my $a = 1;
my $b = '2';
return $a . $b; // '12'

Even though $a is defined as a number while $b is defined as a string, we can put them both into the number-expecting addition operator or the string-expecting concatenation operator and Perl will handle the conversions on their own.  (Although this will still fail if Perl can't perform the conversion - try running the first code after changing $b's string to contain letters.)

On the other hand, Sawtooth Script is strongly typed, so implicit conversions are not allowed.  We provide conversion functions like StringToNumber so users can explicitly convert variable types as needed.
answered Jun 25, 2019 by Zachary Platinum Sawtooth Software, Inc. (215,075 points)
Thanks for the explanation Zachary.

I was testing a 4 digit postcode variable captured as a string.

I was then constructing the postcode into states and Perl treated the postcode string as a number. That was nice.

I was going to use the StringToNumber function to assist with the construction, but in the end, it wasn't needed.

When searching for the Perl equivalent of StringToNumber, I couldn't see it in Sawtooth Software's help, which led me to Perl's int function.

The 2 examples you provided makes sense. I would expect the results of "3" and "12" as you pointed out.

And I sense that Sawtooth Software's SSI Script functions will not allow you to have the same type of freedom as Perl provides. A good reason to have a function like StringToNumber!

Appreciate the tutorial.
And I certainly wouldn't think your suggestion of additional tips, examples, etc. in the help wouldn't go astray.

And improvement in the help is most welcomed.