# Access StringArray Tag elements from script

**URL:** <https://forum.inductiveautomation.com/t/access-stringarray-tag-elements-from-script/15097>\
**Category:** Ignition\
**Tags:** ignition79\
**Created:** [August 17, 2017, 1:36pm UTC](https://forum.inductiveautomation.com/t/access-stringarray-tag-elements-from-script/15097 "2017-08-17T13:36:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![patrick.maltais](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/patrick.maltais/32/3958_2.png) [@patrick.maltais](https://forum.inductiveautomation.com/u/patrick.maltais)\
**Post date:** [August 17, 2017, 1:36pm UTC](https://forum.inductiveautomation.com/t/access-stringarray-tag-elements-from-script/15097/1 "2017-08-17T13:36:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 17, 2017, 3:22pm UTC](https://forum.inductiveautomation.com/t/access-stringarray-tag-elements-from-script/15097/2 "2017-08-17T15:22:59Z")

</div>

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]`.

```auto
stringArray = system.tag.read("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

```

Output:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![patrick.maltais](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/patrick.maltais/32/3958_2.png) [@patrick.maltais](https://forum.inductiveautomation.com/u/patrick.maltais)\
**Post date:** [August 17, 2017, 5:20pm UTC](https://forum.inductiveautomation.com/t/access-stringarray-tag-elements-from-script/15097/3 "2017-08-17T17:20:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 17, 2017, 5:43pm UTC](https://forum.inductiveautomation.com/t/access-stringarray-tag-elements-from-script/15097/4 "2017-08-17T17:43:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![patrick.maltais](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/patrick.maltais/32/3958_2.png) [@patrick.maltais](https://forum.inductiveautomation.com/u/patrick.maltais)\
**Post date:** [August 17, 2017, 6:37pm UTC](https://forum.inductiveautomation.com/t/access-stringarray-tag-elements-from-script/15097/5 "2017-08-17T18:37:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![patrick.maltais](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/patrick.maltais/32/3958_2.png) [@patrick.maltais](https://forum.inductiveautomation.com/u/patrick.maltais)\
**Post date:** [August 17, 2017, 7:05pm UTC](https://forum.inductiveautomation.com/t/access-stringarray-tag-elements-from-script/15097/6 "2017-08-17T19:05:33Z")

</div>

OK I was able to write back using that code:

```auto
stringArray = system.tag.read("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?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [August 17, 2017, 9:19pm UTC](https://forum.inductiveautomation.com/t/access-stringarray-tag-elements-from-script/15097/7 "2017-08-17T21:19:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![patrick.maltais](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/patrick.maltais/32/3958_2.png) [@patrick.maltais](https://forum.inductiveautomation.com/u/patrick.maltais)\
**Post date:** [August 24, 2017, 2:20pm UTC](https://forum.inductiveautomation.com/t/access-stringarray-tag-elements-from-script/15097/8 "2017-08-24T14:20:34Z")

</div>

With this code:

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

```

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

---

<div class="post-metadata">

**Author:** ![MMaynard](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/mmaynard/32/3366_2.png) [@MMaynard](https://forum.inductiveautomation.com/u/MMaynard)\
**Post date:** [August 24, 2017, 3:35pm UTC](https://forum.inductiveautomation.com/t/access-stringarray-tag-elements-from-script/15097/9 "2017-08-24T15:35:09Z")

</div>

You need to put .value on the end of the system.tag.read calls.

> valueSource = system.tag.read(tagSource).value  
> valueDest = system.tag.read(tagDest).value

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [January 3, 2022, 3:21pm UTC](https://forum.inductiveautomation.com/t/access-stringarray-tag-elements-from-script/15097/10 "2022-01-03T15:21:17Z")

</div>

Split this topic on January 3, 2022, 3:21pm UTC

2 posts were split to a new topic: [Access array tag elements with readBlocking](https://forum.inductiveautomation.com/t/access-array-tag-elements-with-readblocking/54604)
