String split function result

how to get array/list row/column count when split functions returns string?

Not quite sure what you are asking. The split function in the expression language gives you back a dataset with one column and rows for every item you split on. So if your string is:

Hello;SomethingElse;56.7

and you split on the semi-colon:split("Hello;SomethingElse;56.7", ";")you would get a dataset with one column and three rows. You can find out how many rows by doing the following:len(split("Hello;SomethingElse;56.7", ";"))You can grab one value out of the dataset like this:split("Hello;SomethingElse;56.7", ";")[1,0]That gets the value out of row 1 and column 0 which would be “SomethingElse”.

If you are using the split function in scripting here is an example:str = "Hello;SomethingElse;56.7" values = str.split(";") print len(values) # number of items print values[1] # gives you back "Something Else"

thanks a lot for the info

Hi Travis, hope you can guide me through my error. I have a script to move data from a string to a list so I can write individual values to memory tags. The code is shown below.

	#Move currentValue to another tag
	string = currentValue
	
	#Tag Paths to list paths.
	pbadge = "[default]CognexEOLInspection/AD01_Cognex/Badge"
	pcflagr = "[default]CognexEOLInspection/AD01_Cognex/C_FlagRH"
	pcflagl = "[default]CognexEOLInspection/AD01_Cognex/C_FlagLH"
	plogo = "[default]CognexEOLInspection/AD01_Cognex/Logo"
	pstatus = "[default]CognexEOLInspection/AD01_Cognex/Status"
	paths =[pbadge, pcflagl, pcflagr, plogo, pstatus]
			
	#Reverse concatenate
	list = string.split(",")
	badge = list[1]
	cflagr = list[3]
	cflagl = list[5]
	logo = list[7]
	status = list[8]
	values = [badge, cflagl, cflagr, logo, status]	
				
	#Assign values to tags.
	system.tag.writeBlocking(paths, values)

Now my problem is that it doesn't run and when I go to the gateway logs it keeps saying

Subject has no attribute 'split'

Any suggestions?

  1. Don't use list or string as variable names
  2. To use split you use it on the string object itself, so it should be `someStringVariable.split(',')

Like

myString = 'some,thing,here'
result = myString.split(',')
print result

prints
['some', 'thing', 'here']

I guess you do have a string = currentValue but I don't think that is the right way to ge tthe currentValue. I think it should be event.currentValue? Something is wrong with this line. Also we don't know the context this is running. It looks like a tag change script, but on what sort of tag? If it's an integer tag for instance, then your current value is just going to be the new integer value of the tag, not the path of the tag and split doesn't exist for integers.

1 Like

I'm assuming that this is in a tagChange script, or perhaps something else, since you didn't say.

That said, I suspect that instaed of currentValue, what you really want is currentValue.value, or some variant depending on context.

I would strongly suggest that you use more meaniful variable names, partcuarly not names that match python built-ins

Just imagine if you needed to use list() to create a list later on in this script and recieved an error telling you that list is not callable?

1 Like

Thank you both. I will work on my variant naming, that said it was a value.change script so yeah I had to add the currentValue.value for it to work. I keep forgetting about it :sweat_smile:

Also, just because I think this is more readable:

paths = [ "[default]CognexEOLInspection/AD01_Cognex/" + tagName for tagName in ('Badge','C_FlagRH','C_FlagLH','Logo','Status')]

system.tag.writeAsync(paths,[currentValue.value.split(',')[i] for i in (1,3,5,7,8)])

Maybe it's just me. :wink: