Award
Congratulations DJ Sures, for answering the question and has received an EZ-Credit award! Share what you have learned and create a tutorial to help others by clicking here.

Expression Parser Broken - Length Function

Assistance Requested

Help ptp with their question and receive $10 of EZ-Credit to get more robots and parts from our store. The following information was provided about their previous efforts searching tutorials for a resolution.

ptp claims to have checked these sources:
 
#1

The code below does not work:

Code:


1: ClearVariables()
2: $buffer = "foo;bar;quux"
3: $sepPos = 3
4: $len0 = Length($buffer)-$sepPos-1



error:

Quote:


Error on line 4: Operator '-' invalid for strings.



1st attempt with a different code:

Code:


1: ClearVariables()
2: $buffer = "foo;bar;quux"
3: $sepPos = 3
4: $len1 = Length($buffer) - $sepPos
5: $len1 = $len1 - $sepPos



same error.

2nd attempt (success)

Code:


1: ClearVariables()
2: $buffer = "foo;bar;quux"
3: $sepPos = 3
4: $len2 = Length($buffer)
5: $len2 = $len2 - $sepPos - 1



it seems the the parser assumes Length function returns a string ?

I have found a few conversations, tutorials and activities that may help with your question. Take a look at these links. I've sorted them by what I believe to be most relevant but that is not always the case as I'm still learning.


Also, consider reviewing the Learn section for informative lessons and activities. Check it out!


#2

Quote:

1st attempt with a different code:

Code:

Code:


1: ClearVariables()
2: $buffer = "foo;bar;quux"
3: $sepPos = 3
4: $len1 = Length($buffer) - $sepPos
5: $len1 = $len1 - $sepPos




I ran into this issue myself, if you don't want to separate it over multiple lines you can cast the length in an int. I guess it is just one of the limitations of not declaring variable types.

Code:

Code:

ClearVariables()
$buffer = "foo;bar;quux"
$sepPos = 3
$len0 = CInt(Length($buffer)) - $sepPos-1
Print( $len0 )

#3

yes is another away to skin the cat...

i had other similar issues with other functions too.

I don't agree with "it's just one of the limitations of not declaring variable types" mainly because of this:

Code:


$rLen1 = IndexOf($buffer, ";")-$len1-1



and it works, one function and two math operations.

Length() is returning the correct type (integer), only when combined behaves differently i'm guessing is really a parser issue.

#4

Code:

 Length() is returning the correct type (integer), only when combined behaves differently i'm guessing is really a parser issue. 

Makes sense.

#5

fixed in next release

#6

Thanks

#7

It seems I'm facing the same issue. I just installed the latest version of EZB...

Code:


$encoderBVAR = $encoderB - $var

if ($encoderA < $encoderBVAR)
$forwardmustwait = 1
$pwmFR = $pwmFR +$mod
$pwmRR = $pwmRR +$mod
$pwmFL = $pwmFL -$mod
$pwmRL = $pwmRL -$mod
$forwardmustwait = 0

endif



Code:


> Error on line 45: Operator '-' invalid for strings.
Done (00:00:00.5009241)

#8

Just a guess here but try

Code:


$encoderBVAR = $encoderB - $var

if ($encoderA < $encoderBVAR)
$forwardmustwait = 1
$pwmFR = $pwmFR + $mod
$pwmRR = $pwmRR + $mod
$pwmFL = $pwmFL - $mod
$pwmRL = $pwmRL - $mod
$forwardmustwait = 0

endif

#9

Elfge, Dave is right. You will need spaces between the operation commands.

#10

lol I just spent 4 minutes looking and I can't figure where my spaces are missing. It's time I take a break I guess... thank you DJ and Dave...