# Iterating multiple conditional statements, for loop

**URL:** <https://forum.inductiveautomation.com/t/iterating-multiple-conditional-statements-for-loop/19505>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [September 3, 2018, 3:45pm UTC](https://forum.inductiveautomation.com/t/iterating-multiple-conditional-statements-for-loop/19505 "2018-09-03T15:45:49Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![agweb.salinas](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@agweb.salinas](https://forum.inductiveautomation.com/u/agweb.salinas)\
**Post date:** [September 3, 2018, 3:45pm UTC](https://forum.inductiveautomation.com/t/iterating-multiple-conditional-statements-for-loop/19505/1 "2018-09-03T15:45:49Z")

</div>

Hey,  
So I’m still trying to improve my knowledge on how ignition datasets work. What i’m practicing with now is taking one table and distributing it into 5 different tables through a series of conditional statements.

the goal of the code is when I press a button the code,

1. reads data from Sampler column.
2. compares row value in column 6 to conditional values.
3. appends entire row to respective data table.
4. changes to the new window

```auto
sourceTable = event.source.parent.getComponent('Table')
dataOut = sourceTable.data
pythonDataset = system.dataset.toPyDataSet(dataOut)

header = ["col1", "col2", "col3", "col4", "col5",
 "col6", "Sampler", "col7", "col8"]

NewData = []
for row in dataOut:
		if row[6]== "Sampler 1":
			NewData.append(row)
			SamData = system.dataset.toDataSet(header, NewData)
			system.gui.getWindow("advance").rootContainer.getComponent("TableT1").data = SamData
		elif row[6]== "Sampler 2":
			NewData.append(row)
			SamData = system.dataset.toDataSet(header, NewData)
			system.gui.getWindow("advance").rootContainer.getComponent("TableT2").data = SamData
		elif row[6]== "Sampler 3":
			NewData.append(row)
			SamData = system.dataset.toDataSet(header, NewData)
			system.gui.getWindow("advance").rootContainer.getComponent("TableT3").data = SamData
		elif row[6]== "Sampler 4":
			NewData.append(row)
			SamData = system.dataset.toDataSet(header, NewData)
			system.gui.getWindow("advance").rootContainer.getComponent("TableT4").data = SamData
		elif row[6]== "Sampler 5":
			NewData.append(row)
			SamData = system.dataset.toDataSet(header, NewData)
			system.gui.getWindow("advance").rootContainer.getComponent("TableT5").data = SamData
			
window = system.nav.openWindow('advance')
system.nav.centerWindow(window)
system.nav.closeParentWindow(event)

```

This is the Error I get from the Code:

Traceback (most recent call last):  
File “event:actionPerformed”, line 15, in   
TypeError: ‘com.inductiveautomation.ignition.common.BasicDataset’ object is not iterable

---

<div class="post-metadata">

**Author:** ![Kevin.Herron](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/kevin.herron/32/7259_2.png) [@Kevin.Herron](https://forum.inductiveautomation.com/u/Kevin.Herron)\
**Post date:** [September 3, 2018, 4:00pm UTC](https://forum.inductiveautomation.com/t/iterating-multiple-conditional-statements-for-loop/19505/2 "2018-09-03T16:00:57Z")

</div>

You should be iterating over your newly-created `pythonDataset`, not the original one:

`for row in pythonDataset:`

---

<div class="post-metadata">

**Author:** ![agweb.salinas](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@agweb.salinas](https://forum.inductiveautomation.com/u/agweb.salinas)\
**Post date:** [September 3, 2018, 4:08pm UTC](https://forum.inductiveautomation.com/t/iterating-multiple-conditional-statements-for-loop/19505/3 "2018-09-03T16:08:45Z")

</div>

@Kevin.Herron Oh Thank you! I forgot I made that change from my original code. Unfortunately its still throwing the same error.

---

<div class="post-metadata">

**Author:** ![agweb.salinas](https://avatars.discourse-cdn.com/v4/letter/a/3ec8ea/32.png) [@agweb.salinas](https://forum.inductiveautomation.com/u/agweb.salinas)\
**Post date:** [September 3, 2018, 6:58pm UTC](https://forum.inductiveautomation.com/t/iterating-multiple-conditional-statements-for-loop/19505/4 "2018-09-03T18:58:18Z")

</div>

I believe the problem is I am trying to iterate a dataset and datasets are not iterable but I’m unsure how to get around this with the same result.

---

<div class="post-metadata">

**Author:** ![c.bertulli](https://avatars.discourse-cdn.com/v4/letter/c/f19dbf/32.png) [@c.bertulli](https://forum.inductiveautomation.com/u/c.bertulli)\
**Post date:** [September 4, 2018, 8:49am UTC](https://forum.inductiveautomation.com/t/iterating-multiple-conditional-statements-for-loop/19505/5 "2018-09-04T08:49:52Z")

</div>

I think the issue here is that `system.dataset.toDataset` wants two lists: one of string and the other of strings/numbers. You are passing a list of string ( Header ) but for the other argument, you are passing a list of PyRowObject instead of strings/numbers.

If you want to solve your problem I think you have to get the value in every row and append it inside a list. Something like this:

```auto
NewData = []
for row in pythonDataset:
	
		if row[6]== "Sampler 1":
			for Value in row:
				val = pythonDataset.getValueAt(Value,6)
				NewData.append(val)
			
			SamData = system.dataset.toDataSet(header, NewData)
			system.gui.getWindow("advance").rootContainer.getComponent("TableT1").data = SamData

```

I did not test the code, but it should be something similar.  
Hope this helps

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [September 4, 2018, 11:33am UTC](https://forum.inductiveautomation.com/t/iterating-multiple-conditional-statements-for-loop/19505/6 "2018-09-04T11:33:27Z")

</div>

I don’t know what the original table contains, but you are also writing an entirely new dataset at every pass of the loop. This would give the net effect of having a single line with the latest value from the original table. You would actually need five different data lists. Also to expand on @c.bertulli’s mention, you would need to make a list out of the row:

```auto
sourceTable = event.source.parent.getComponent('Table')
dataOut = sourceTable.data
pythonDataset = system.dataset.toPyDataSet(dataOut)

header = ["col1", "col2", "col3", "col4", "col5", "col6", "Sampler", "col7", "col8"]

NewData1 = []
NewData2 = []
NewData3 = []
NewData4 = []
NewData5 = []

for row in dataOut:

	#convert row to list
	rowData = list(row)

	if row[6]== "Sampler 1":
		NewData1.append(rowData)
	elif row[6]== "Sampler 2":
		NewData2.append(rowData)
	elif row[6]== "Sampler 3":
		NewData3.append(rowData)
	elif row[6]== "Sampler 4":
		NewData4.append(rowData)
	elif row[6]== "Sampler 5":
		NewData5.append(rowData)

SamData1 = system.dataset.toDataSet(header, NewData1)
system.gui.getWindow("advance").rootContainer.getComponent("TableT1").data = SamData1

SamData2 = system.dataset.toDataSet(header, NewData2)
system.gui.getWindow("advance").rootContainer.getComponent("TableT2").data = SamData2

SamData3 = system.dataset.toDataSet(header, NewData3)
system.gui.getWindow("advance").rootContainer.getComponent("TableT3").data = SamData3

SamData4 = system.dataset.toDataSet(header, NewData4)
system.gui.getWindow("advance").rootContainer.getComponent("TableT4").data = SamData4

SamData5 = system.dataset.toDataSet(header, NewData5)
system.gui.getWindow("advance").rootContainer.getComponent("TableT5").data = SamData5

window = system.nav.openWindow('advance')
system.nav.centerWindow(window)
system.nav.closeParentWindow(event)

```

And, for those that like to make their code as compact as possible 😏:

```auto
sourceTable = event.source.parent.getComponent('Table')
dataOut = sourceTable.data
pythonDataset = system.dataset.toPyDataSet(dataOut)

samplerList = ['Sampler 1','Sampler 2','Sampler 3','Sampler 4','Sampler 5']
tableList = ['TableT1','TableT2','TableT3','TableT4','TableT5']

header = ["col1", "col2", "col3", "col4", "col5", "col6", "Sampler", "col7", "col8"]
NewData = [[]] * 5 #create list of five empty lists

for row in dataOut:
	try:
		#convert row to list
		rowData = list(row)
		NewData1[samplerList(row[6])].append(rowData)
	except:
		print str(row[6]) + ' is not in samplerList'

for table, data in zip(tableList, NewData)
	system.gui.getWindow("advance").rootContainer.getComponent(table).data = system.dataset.toDataSet(header, data)

window = system.nav.openWindow('advance')
system.nav.centerWindow(window)
system.nav.closeParentWindow(event)

```
