# Variable in component property

**URL:** <https://forum.inductiveautomation.com/t/variable-in-component-property/9558>\
**Category:** Archived Topics\
**Created:** [January 17, 2015, 6:39am UTC](https://forum.inductiveautomation.com/t/variable-in-component-property/9558 "2015-01-17T06:39:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![zfellers](https://avatars.discourse-cdn.com/v4/letter/z/b19c9b/32.png) [@zfellers](https://forum.inductiveautomation.com/u/zfellers)\
**Post date:** [January 17, 2015, 6:39am UTC](https://forum.inductiveautomation.com/t/variable-in-component-property/9558/1 "2015-01-17T06:39:19Z")

</div>

I have a table with manually entered values. I’m attempting to write each “cell” to a tag using button component scripting. Using for loops I’m able to map nearly the entire matrix to individual tags, however the final column is a summation linked to custom properties with expression binding dependent on column values entered by the user. The code below is incorrect:

[code]rows = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]  
cols = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]  
pathMat = “MAT/Lines/”  
linesTrackingData = event.source.parent.getComponent(“Material Tracking”).data  
pathTotal = “MAT/Lines/Total/Total”  
linesTracking = event.source.parent.getComponent(“Material Tracking”)

for row in rows:  
tempTotal = “linesTracking.Total” + str(row)  
system.tag.write(pathTotal + str(row), float(tempTotal))  
for col in cols:  
tempMat = pathMat + str(row) + “/” + str(col)  
system.tag.write(tempMat, linesTrackingData.getValueAt(row, col))[/code]

I need to insert a variable (row) into the component’s custom property name within the for loop to avoid coding 24 individual tag assignments.

---

<div class="post-metadata">

**Author:** ![ian](https://avatars.discourse-cdn.com/v4/letter/i/bb73d2/32.png) [@ian](https://forum.inductiveautomation.com/u/ian)\
**Post date:** [September 9, 2015, 5:33am UTC](https://forum.inductiveautomation.com/t/variable-in-component-property/9558/2 "2015-09-09T05:33:47Z")

</div>

Just to clarify: are you saying that you bascially want to reduce the code from double for loops to single for loop?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [September 9, 2015, 1:31pm UTC](https://forum.inductiveautomation.com/t/variable-in-component-property/9558/3 "2015-09-09T13:31:08Z")

</div>

you need to use the python builtin ‘getattr()’ to look up a property by string.[code]rows = range(24)  
cols = range(2, 12)  
pathMat = “MAT/Lines/”  
linesTrackingData = event.source.parent.getComponent(“Material Tracking”).data  
pathTotal = “MAT/Lines/Total/Total”  
linesTracking = event.source.parent.getComponent(“Material Tracking”)

for row in rows:  
tempTotal = “Total” + str(row)  
system.tag.write(pathTotal + str(row), float(getattr(linesTracking, tempTotal)))  
for col in cols:  
tempMat = pathMat + str(row) + “/” + str(col)  
system.tag.write(tempMat, linesTrackingData.getValueAt(row, col))[/code]If you find this routine sluggish, consider batching the tag writes using [writeAll()](https://support.inductiveautomation.com/usermanuals/ignition/system_tag_writeall.htm), like so:[code]rows = range(24)  
cols = range(2, 12)  
pathMat = “MAT/Lines/”  
linesTrackingData = event.source.parent.getComponent(“Material Tracking”).data  
pathTotal = “MAT/Lines/Total/Total”  
linesTracking = event.source.parent.getComponent(“Material Tracking”)

for row in rows:  
tempTotal = “Total” + str(row)  
tagPaths = [pathTotal + str(row)]  
tagValues = [float(getattr(linesTracking, tempTotal))]  
for col in cols:  
tagPaths.append(pathMat + str(row) + “/” + str(col))  
tagValues.append(linesTrackingData.getValueAt(row, col))  
system.tag.writeAll(tagPaths, tagValues)[/code]
