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

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')

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

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

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.

# 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

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

here is some standerd filter function

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:

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

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?

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

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.

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.

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.

1 Like

This works, thank you!