Access StringArray Tag elements from script

I’m looking for a way to access individual elements inside a tag that has been configured as StringArray. I want to access it from within a script. How can I do that? In fact, I need to know the uper bound which could be anything. So how could I know the size of the array programmatically?

Here are three (of many) possible ways to get each element in the array. Python actually makes it easier to get the the individual element than do get an index in the loop (that’s what the enumerate() call is for, in the third example). As you can see, individual elements can be reference with stringArray[index].

stringArray ="StringArray").value
print 'Length:', len(stringArray)

for element in stringArray:
	print 'Option 1:', element
for index in range(len(stringArray)):
	print 'Option 2:', stringArray[index]
for index, element in enumerate(stringArray):
	print 'Option 3:', index, element


Length: 6
Option 1: a
Option 1: b
Option 1: c
Option 1: d
Option 1: e
Option 1: f
Option 2: a
Option 2: b
Option 2: c
Option 2: d
Option 2: e
Option 2: f
Option 3: 0 a
Option 3: 1 b
Option 3: 2 c
Option 3: 3 d
Option 3: 4 e
Option 3: 5 f
1 Like

How about writing back in each element? Suppose I want to clear the array and write an empty string in each element?

system.tag.write("StringArray", [""] * len(current))

1 Like

Sorry I ment a specific element to write in the array.

OK I was able to write back using that code:

stringArray ="StringArray").value
for index in range(len(stringArray)):
	element = "StringArray[" + str(index) + "]"
	system.tag.write(element, "")

I would use itemCount = len(stringArrays) to know the exact length of the array and make sure not to write in beyond the upper bound.

One thing I don’t understand. I have to create the tag manually before using it in the system.tag.write() function. I would expect system.tag.write("stringArray[" + str(index) + "]") to work but it’s not the case. Why?

You can also write back to individual elements without blanking the rest of the array, if that’s what you’re after:
system.tag.write("StringArray[%s]" % 1, "Test")

Also, doing the string formatting of the tagpath inline should definitely work - although I notice that in your example code you’re not passing any actual value in to system.tag.write - even if you’re writing an empty string, you have to specifically pass it in as a value:
system.tag.write("stringArray[" + str(index) + "]", "") or:
system.tag.write("stringArray[%s]" % index, "")

The %s is a Python cue for string replacement - it’s generally recommended for cleaner string formatting.

1 Like

With this code:

tagSource = "[ProviderX]SomeUDDTInstance/Message[%s]" % idx
tagDest = "[ProviderX]SomeUDDTInstance/Message[%s]" % (idx + 1)
valueSource =
valueDest =
print tagSource
print tagDest

Why do I print the name of the tag instead of its value?

You need to put .value on the end of the calls.

valueSource =
valueDest =

2 posts were split to a new topic: Access array tag elements with readBlocking