# How to loop through two columns row by row and compare row values?

**URL:** <https://forum.inductiveautomation.com/t/how-to-loop-through-two-columns-row-by-row-and-compare-row-values/76779>\
**Category:** Ignition\
**Created:** [July 24, 2023, 1:54pm UTC](https://forum.inductiveautomation.com/t/how-to-loop-through-two-columns-row-by-row-and-compare-row-values/76779 "2023-07-24T13:54:57Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [July 24, 2023, 1:54pm UTC](https://forum.inductiveautomation.com/t/how-to-loop-through-two-columns-row-by-row-and-compare-row-values/76779/1 "2023-07-24T13:54:57Z")

</div>

I have a table with 2 columns [city, number], every time a new city and number is entered, I want to check it against the previous row's city and numbers. If the new row's values is different than the previous, I want to open a popup. For example if first row is [NYC, 101] and the next is [Dallas, 43] the popup will open. If the next row is [Dallas, 43] nothing happens, if the 4th is [Houston, 22] the popup opens.  
This is what I have right now but i'm not sure how to compare new row to previous row

```
cityNum = system.db.runNamedQuery('getTable') #just an example, returns 2 columns city and num
i = 0
if cityNum.getRowCount() >= 0:
	while i < cityNum.getRowCount():
      #I was thinking maybe something like this.. if cityNum.getValueAt(i, 'City', 'Num')

```

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [July 24, 2023, 2:25pm UTC](https://forum.inductiveautomation.com/t/how-to-loop-through-two-columns-row-by-row-and-compare-row-values/76779/2 "2023-07-24T14:25:15Z")

</div>

you are just looking of there is a duplicate [city, number]?

---

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [July 24, 2023, 2:37pm UTC](https://forum.inductiveautomation.com/t/how-to-loop-through-two-columns-row-by-row-and-compare-row-values/76779/3 "2023-07-24T14:37:04Z")

</div>

The opposite really. Nothing happens if there's a duplicate.

---

<div class="post-metadata">

**Author:** ![CalebM](https://avatars.discourse-cdn.com/v4/letter/c/71c47a/32.png) [@CalebM](https://forum.inductiveautomation.com/u/CalebM)\
**Post date:** [July 24, 2023, 2:48pm UTC](https://forum.inductiveautomation.com/t/how-to-loop-through-two-columns-row-by-row-and-compare-row-values/76779/4 "2023-07-24T14:48:20Z")

</div>

In this case we loop through the dataset checking the if the previous value is equal to the current value. Dataset have a bit of a learning curve at first, but the user manual is pretty helpful: [Datasets - Ignition User Manual 8.0 - Ignition Documentation](https://docs.inductiveautomation.com/display/DOC80/Datasets).

```auto
# Create sample data
headers = ['city', 'number']
rows = [
['NYC', 101],
['Dallas', 43],
['Dallas', 43],
['Houston', 22]
]
cityNum = system.dataset.toDataSet(headers, rows)

prevCity = None
prevNumber = None
# Iterate over dataset
for row in range(cityNum.getRowCount()):
	# Get city and number for current row
	city = cityNum.getValueAt(row, 'city')
	number = cityNum.getValueAt(row, 'number')
	
	# Check if city and number are different from last row
	if not (prevCity == city) and not (prevNumber == number):
		#Open Popup Here
		print city, number
	
	# Update previouse city and number
	prevCity = city
	prevNumber = number

```

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [July 24, 2023, 2:49pm UTC](https://forum.inductiveautomation.com/t/how-to-loop-through-two-columns-row-by-row-and-compare-row-values/76779/5 "2023-07-24T14:49:05Z")

</div>

So you just want to filter and see if the filtered ds is empty xd

here is some standerd filter function

```auto
def filterDataset(ds, **kwargs):
	pds = system.dataset.toPyDataSet(ds)
	output = []
	for row in pds:
		if all(row[key] == value for key, value in kwargs.items()):
			output.append(row)
	
	return system.dataset.toDataSet(list(ds.columnNames), output)

```

used like:

```auto
filtered = filterDataset(ds, city=newRow.city, number=newRow.number)

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 24, 2023, 3:24pm UTC](https://forum.inductiveautomation.com/t/how-to-loop-through-two-columns-row-by-row-and-compare-row-values/76779/6 "2023-07-24T15:24:26Z")

</div>

Why only the previous row? Why not all rows? Do you want a popup if the new 4th row matches the 1st row but not the 3rd row?

---

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [July 24, 2023, 4:07pm UTC](https://forum.inductiveautomation.com/t/how-to-loop-through-two-columns-row-by-row-and-compare-row-values/76779/7 "2023-07-24T16:07:47Z")

</div>

Even if the 4th row matches the 1st, if it doesn't match the 3rd, I want a popup

---

<div class="post-metadata">

**Author:** ![humphrey.daniel](https://avatars.discourse-cdn.com/v4/letter/h/8e8cbc/32.png) [@humphrey.daniel](https://forum.inductiveautomation.com/u/humphrey.daniel)\
**Post date:** [July 24, 2023, 4:18pm UTC](https://forum.inductiveautomation.com/t/how-to-loop-through-two-columns-row-by-row-and-compare-row-values/76779/8 "2023-07-24T16:18:46Z")

</div>

Consider starting the data entry process with a pop-up (I'm more of a pane guy myself). Supply the data entry interface with the last row of data and do whatever logic you have in mind.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [July 24, 2023, 4:51pm UTC](https://forum.inductiveautomation.com/t/how-to-loop-through-two-columns-row-by-row-and-compare-row-values/76779/9 "2023-07-24T16:51:33Z")

</div>

How is the user entering the new data?

Assuming that data is being entered directly into the table component, then all you need is a simple comparison, no need for loops, at all.

```auto
ds = system.dataset.toPyDataSet(system.db.runNamedQuery(‘getTable’))

newRow = (ds[ds.rowCount-1][0],ds[ds.rowCount-1][1])
lastRow = (ds[ds.rowCount-2][0],ds[ds.rowCount-2][1])

if newRow != lastRow:
    #showPopUp

```

NOTE: This code was entered from my phone it will not work due to the wrong type of single quotes. It is also untested and presented here as only an example.

---

<div class="post-metadata">

**Author:** ![Christy\_Jean](https://avatars.discourse-cdn.com/v4/letter/c/977dab/32.png) [@Christy\_Jean](https://forum.inductiveautomation.com/u/Christy_Jean)\
**Post date:** [July 24, 2023, 5:18pm UTC](https://forum.inductiveautomation.com/t/how-to-loop-through-two-columns-row-by-row-and-compare-row-values/76779/10 "2023-07-24T17:18:49Z")

</div>

This works, thank you!
