-->
Page 1 of 1

LEN()

PostPosted: Mon Apr 11, 2016 6:35 am
by Electroguard
To summarise several days painful fault-tracing into a few calm words...

len() works ok with string literals "like this" but not with string variables because they cause an esp reset.

The attached bit of code demonstrates the problem which exists on my setup, so I hope will also be replicated for others. My esp is being used in AP mode in case that has any relevence.

Intrepid Bug Hunters be warned, this monster has no respect for authority and rampages through both Alpha 7 and 9.

It's powers seem to strengthen the deeper it is in a long script, and it can attack from the most unlikely and innocent places, such as within the string quotes of udpreply " the len monster could even be lurking in here to cause an esp reset".

It's hiding place is not consistent, so I dedicated a whole session to changing EVERY occurance of the 'len'gth characters into some alternative string, ie: "lngth" to prevent some weird esp resets.

Code: Select allprint len("123") & len("ABCEF") & "  This line proves that len() can do string literals"

let number = 123 + 4 ' just a test number proving its a number
let length = 10 - 1 ' just a test length number proving it is a number
let string = str(number)
print "number=" & number & "  length number=" & length & "string of number=" & string

print "next line tries to do len() of string variable but causes esp to reset if uncommented"
' let length = len(string)  'uncomment this line to show that len() cannot do string variables"
print "length=" & length & "   just to prove that the problem is caused by the line above"

Re: LEN()

PostPosted: Mon Apr 11, 2016 10:08 am
by cicciocb
Hi, I know where the problem comes from.
In fact all the variables are stored as string and the parser try to identify the format in function of the content.
So, in your case, the content of the string is a number so it's treated as a number!
try this :
Code: Select alllet string = str(number) & "_"

In this case the len function will work as the variable will be identified as a string.
So, for the moment, the only way is to cheat (like adding an '_' at the end).
I think it's time to add a mechanism to identify if the variable is a string or a number.
Update in next releases .....

Re: LEN()

PostPosted: Mon Apr 11, 2016 10:45 am
by Electroguard
Thanks, that helps.
It's for doing an ip blinking routine (you wouldn't believe the blinking grief than len() has caused me) but now that you've shown me how to get around it, I can progress, ta.

Re: LEN()

PostPosted: Tue Apr 12, 2016 3:26 am
by cicciocb
Hi,
last load (2.0.Alpha 10) rectify some snags, including the famous len().
We changed the way the variables are now handled, now a = "1234' and a = 1234 is not the same, the strings are managed as strings.
So the good news is that the len() and all the string functions (mid, left, ...) work properly even if the argument is a 'numerical string' like "123456".
We reinforced also the check of the arguments so now, trying to give numerical input to string function, will raise an error message instead of crash.
You can try
a = 1234
print len(a)
a = "1234"
print len(a)
Looking into the VARS page, the strings are now printed with quotes.