# Trouble with Looping

**URL:** <https://forum.inductiveautomation.com/t/trouble-with-looping/34920>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [May 20, 2020, 6:42pm UTC](https://forum.inductiveautomation.com/t/trouble-with-looping/34920 "2020-05-20T18:42:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bbranison](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@bbranison](https://forum.inductiveautomation.com/u/bbranison)\
**Post date:** [May 20, 2020, 6:42pm UTC](https://forum.inductiveautomation.com/t/trouble-with-looping/34920/1 "2020-05-20T18:42:44Z")

</div>

My loop script is iterating too many times. The first return result is fine, but the second is the total of the 1st and 2nd, then the 3rd is the total of the 2nd and 3rd, and so on.

What I expect to get:  
15 152  
67 101  
7 161  
7 161  
13 155  
13 155

What I’m getting:  
15 152  
82 253  
89 414  
96 575  
109 730  
122 885

Script:  
‘’’  
rawDS = data[“named\_query”].getCoreResults()  
machines = []  
tags = system.tag.browseTags(parentPath="[default]CoorsTek/Benton/Furnaces", tagType = “UDT\_INST”)  
for tag in tags:  
machines.append(tag.name)  
machinesSorted = sorted(machines)   
headers = [‘Equipment’,‘Uptime’,‘Downtime’]  
upList = []  
downlist = []  
upTimeList = []  
downTimeList = []

```
for x in machinesSorted:
	for row in range(rawDS.getRowCount()):
		if rawDS.getValueAt(row,x) == 1:
			upList.append(rawDS.getValueAt(row,x))
		if rawDS.getValueAt(row,x) == 3:
			downlist.append(rawDS.getValueAt(row,x))
	downHours = len(downlist)/60	
	upHours = len(upList)/60
	upTimeList.append(upHours)
	downTimeList.append(downHours)

dsList = zip(machinesSorted,upTimeList,downTimeList)	
filteredDS = system.dataset.toDataSet(headers,dsList)	
data["Report Basic Data"] = filteredDS 

```

‘’’  
The items in the “machinesSorted” list match the names of columns in “rawDS”. Each row in rawDS is a machine status integer every minute.

Can anyone see where I went astray with my for statements? Any help would be much appreciated.

---

<div class="post-metadata">

**Author:** ![bpreston](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@bpreston](https://forum.inductiveautomation.com/u/bpreston)\
**Post date:** [May 20, 2020, 6:59pm UTC](https://forum.inductiveautomation.com/t/trouble-with-looping/34920/2 "2020-05-20T18:59:32Z")

</div>

If I had to guess you are creating your uplist and downlist at the start but need to clear it on each loop of machinesSorted.

```auto
rawDS = data[“named_query”].getCoreResults()
machines = []
tags = system.tag.browseTags(parentPath="[default]CoorsTek/Benton/Furnaces", tagType = “UDT_INST”)
for tag in tags:
machines.append(tag.name)
machinesSorted = sorted(machines)
headers = [‘Equipment’,‘Uptime’,‘Downtime’]

upTimeList = []
downTimeList = []

for x in machinesSorted:
    upList = []
    downlist = []
	for row in range(rawDS.getRowCount()):
		if rawDS.getValueAt(row,x) == 1:
			upList.append(rawDS.getValueAt(row,x))
		if rawDS.getValueAt(row,x) == 3:
			downlist.append(rawDS.getValueAt(row,x))
	downHours = len(downlist)/60	
	upHours = len(upList)/60
	upTimeList.append(upHours)
	downTimeList.append(downHours)

dsList = zip(machinesSorted,upTimeList,downTimeList)	
filteredDS = system.dataset.toDataSet(headers,dsList)	
data["Report Basic Data"] = filteredDS 

```

---

<div class="post-metadata">

**Author:** ![bbranison](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@bbranison](https://forum.inductiveautomation.com/u/bbranison)\
**Post date:** [May 20, 2020, 7:46pm UTC](https://forum.inductiveautomation.com/t/trouble-with-looping/34920/3 "2020-05-20T19:46:33Z")

</div>

🙌

That was it. Thank you!
