String Manipulation

I am trying to manipulate a string from an OPC tag using the Left function. I am able to return the full string without issue. After several failed attempts, I copied the text from the Hello World example given for the left function as a test and it also returned an error stating “NameError: name ‘left’ is not defined”. Am I missing something?

Expression from example:
x = left(“hello”, 2) - I put the x = into the expression.

It sound like you are trying to use this in a script. Maybe a bit more detail on what you’re trying to do is in order.

Just to iterate, scripting (Jython) doesn’t have a left function. Instead you would use what’s called slicing:

Here’s an overview in the Python documentation on strings

I am reading the Windows machine name from several clients. Each client’s machine name is in the form of xxxx_yyy_Name. The only portion I am interested in using is the “Name”. I got the left function from Appendix B -> Strings -> Left from the Help menu.

This is the example given in the help.

left(string, charCount)

Returns count characters from the left side of string, where count and string are the arguments to the function.

left("hello", 2)

...returns "he"

left("hello", 0)

...returns ""

left("hello", 5)

...returns "hello"

This is what I was using to test.

strMachine_Name ="[System]Client/Network/Hostname")
strClientName = left(strMachine_Name.value, 9)

You need to understand the difference between Jython scripting and Igntion’s built in expression language.

In expression language “left” is a valid function however you can not create and assign values to variables. If you want to assign the left 5 chars in a binding you simply put left(“hello world”, 5) in the expression.

Scripting is actual Jython code and you have to use string slicing as Jordan indicated.

myString = "hello world"
x = myString[:5]
print x

Are you trying to get the machine name in an expression or in an event script? Once you determine the answer to that question we can be of more assistance as needed.

Edit: It looks like you are trying to use Jython scripting.

This is what you need: (I also removed the unnecessary parenthesis in the print statement)

strMachine_Name ="[System]Client/Network/Hostname")
strClientName = strMachine_Name.value[9:]
print strClientName
1 Like

Thank you! This is exactly what I am looking for.