Nested Functions

Why does one way work and the other gives a syntax error, see below.

Works:

sTest = chr(49);
nTest = StringToInt32(sTest);

Does not work:

nTest = StringToInt32(chr(49));

chr(49) returns a character and sTest is a string. These are different datatypes.

The difference can be seen in OptoScript: a character is written with an apostrophe like ‘A’ and a string with quotes “A”.

StringToInt32() only accepts strings.

The assignment to a string from a character performs an implicit datatype conversion for you, but this doesn’t happen when a character is used as a parameter. Not sure why on that.

Okay, I dove into this a bit more and I am conjecting a bit, but I think it is reasonable.

First chr is not a function, it is a keyword.

‘A’ is an integer
“A” is a string
chr(‘A’) creates the string “A”, however since strings use variable amounts of memory, the storage location of the string needs to be defined.

This is the case with string concatenation operations as well - and may be clearer to understand. You can’t have this:

nTest = StringToInt32(sTest + “more stuff”);

Where would this new string be located in memory? Imagine if your strategy was large and already using all the available memory and then you created this new string?

Therefore, the concatenation needs to be assigned to a string variable which has a predetermined size.

Here is some Python code that does what I’m asking, see below.

Convert ASCII decimal value 49 to ASCII character

ascii_char = chr(49)

Convert ASCII character back to integer

integer_val = ord(ascii_char)

Do both at one time

integer_val_quick = ord(chr(49))

print(f"ASCII decimal value 49 is represented by the ASCII character ‘{ascii_char}’“)
print(f"The corresponding integer value of ‘{ascii_char}’ is {integer_val}”)
print(f"The corresponding integer value of ‘{ascii_char}’ is {integer_val_quick}")

The printed output is:

ASCII decimal value 49 is represented by the ASCII character ‘1’
The corresponding integer value of ‘1’ is 49
The corresponding integer value of ‘1’ is 49

So why is Python able to do it and not Optoscript?

Dale

Optoscript cannot allocate memory at runtime.

Different tools, different purpose.

1 Like

It is a shame that Optoscript cannot allocate memory at runtime. All other scripting languages I have used, i.e. Python, Perl, VBscript, R, etc… Optoscript is the first exception I have run across.

It would be nice to add this in an upcoming release.

Thanks for your help.

Dale

If your using a RIO or EPIC feel free to download and install the Shell license and program in Python.
Just be sure to use MMP to move your data around as needed as its a lot faster than REST.

https://developer.opto22.com/epicdev/python/

Thanks for the information on MMP’s speed. I have a working Shell license and have successfully interfaced with MMP. I may go in that direction.