Asked — Edited

Using Substring With An Array

Again I cannot see what I am doing wrong. Is this another bug or am I using the array incorrectly? This works


DefineArray($TestArray,2)
$SearchString = "aaaa"
$TestArray[1] = "<aaaa><bbbb><cccc>"

if($SearchString = SubString($TestArray[1],1,4))

Print("Correct")

endif


This Dont


if($SearchString = SubString($TestArray[1],1,IndexOf($TestArray[1],"><")))


ARC Pro

Upgrade to ARC Pro

Harnessing the power of ARC Pro, your robot can be more than just a simple automated machine.

#1  

You have one too many closing brackets in this line...


if($SearchString = SubString($TestArray[1],1,4)))

Edit never mind you fixed it...

PRO
USA
#2  

@RichardR I tried to simplify the expression, and didn't remove the extra closing bracket. But when I did simplify it, it did work.

PRO
USA
#3  

Still have the issue with the IndexOf with in the expression

PRO
USA
#4  

If I use a variable in the expression it works.


DefineArray($TestArray,2)
$SearchString = "aaaa"
$TestArray[1] = "<aaaa><bbbb><cccc>"

$TestIndex = IndexOf( $TestArray[1],1,"><" ) -1

if($SearchString = SubString($TestArray[1],1,$TestIndex))

Print("Correct")

endif

PRO
USA
#5  

@rz90208

EZ-Script is good for simple scripts, and i believe it was developed for simple statements.

there are no expression trees or byte code translation the script lines/statements are evaluated on the fly using a text parser and the things can get complicated with nested conditions, multiples arguments, operators etc.

with that in mind you have some options:

  1. Simplify the expressions like using variables, remember everything is evaluated on the fly so replace multiple function calls assigning the function result to a variable and reusing the variable to check the result.

  2. Avoid nested expressions

  3. Avoid complex scripts when possible

OR

  1. Learn c# and create a plugin

  2. use the DJ's Javascript plugin:

https://synthiam.com/redirect/legacy?table=plugin&id=224

the plugin uses a Javascript Interpreter for .NET (Jint) https://github.com/sebastienros/jint

Is not fast as c# code but it's well known, and if you find a language/parser issue you can open a ticket.

PRO
USA
#6  

Thanks ptp, I did just that and simplified the script using variables. It is not easy to build even a simple brain and not write complex scripts. I do know some JavaScript, would be nice to have a script manager for js as we do for ez-script. Thanks again for your reply.

PRO
Synthiam
#7  

This code works perfect


definearray($testarray, 2)

$searchstring = "aaaa"

$testarray[1] = "<aaaa><bbbb><cccc>"

IF ($searchString = substring($testarray[1], 1, 4))

  print("correct")

ENDIF

User-inserted image

And so does this, by adding spaces (for some reason) with proper code formatting


DefineArray($TestArray,2)
$SearchString = "aaaa"
$TestArray[1] = "<aaaa><bbbb><cccc>"

if($SearchString = SubString($TestArray[1], 1, IndexOf($TestArray[1], "><")))

Print("Correct")

endif

User-inserted image

If you wish to use javascript, use the javascript plugin from here: https://synthiam.com/redirect/legacy?table=plugin&id=224

PRO
USA
#8  

@DJ,

To help troubleshoot:


DefineArray($ArrayOfStrings,2)
$ArrayOfStrings[0] = "foo;bar;baz"
$SearchString = "foo"

#this works
$index1=IndexOf($ArrayOfStrings[0],";" )
if($SearchString=SubString($ArrayOfStrings[0],0,$index1,";" ))
  print("1: starts with foo" )
endif

#this works too
$String1=$ArrayOfStrings[0]
if($SearchString=SubString($String1,0,IndexOf($String1,";" )))
  print("2: starts with foo" )
endif

#The code below works with a space before the IndexOf
if($SearchString=SubString($ArrayOfStrings[0],0, IndexOf($ArrayOfStrings[0],";" ) ))
  print("3: starts with foo" )
endif

#The code below does not work
#Error on line 24: Variable is an array: $ArrayOfStrings
if($SearchString=SubString($ArrayOfStrings[0],0,IndexOf($ArrayOfStrings[0],";" )))
  print("4: starts with foo" )
endif

EDIT: I've changed the code with several combinations, a space before the IndexOf does the trick

PRO
Synthiam
#9  

Okay - i fixed it for next release. There was a goof in the regex

User-inserted image

PRO
USA
#10  

Thank you again DJ. My son tells me to start using JavaScript also, but I love using EZ-Script it is so EZ and powerful.

Thank you also ptp