Variable in component property

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.

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

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(), 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]