How to return a row and column of specific data from a dataset

Good Morning.

My name is Felipe. I have spent time finding a solution for an application, but without success. The idea is that every time the dataset has the word “Label Exchance” or even the code “500”, the system returns the row and column they are in, it can be a label, for example. See attached image

Thank You

I would use the view() expression function from my Simulation Aids module.

Add a custom dataset property to your table with this expression:

view("Select _r As rowIndex Where Reason_Code==500", {Root Container.path.to.table.data})

Or you can use a propertyChange script to search the dataset.

@pturmel was not authorized to install the module. How do I do using a PropertyChange script?

# Creaste sample dataset
sampleHeaders = ['letter', 'number']
sampleData = [['A', 1],
              ['B', 2],
              ['A', 3],
              ['B', 4],
              ['C', 5],
              ['A', 6],
              ['B', 7],
              ['C', 8],
              ['D', 9],
              ['A', 10],
              ['B', 11],
              ['C', 12],
              ['D', 13],
              ['E', 14]
             ]       
dataSet = system.dataset.toDataSet(sampleHeaders, sampleData)

# -----------------------------------------------------------#

def findEquals(datasetIn, colName, value):
	# Convert to PyDataSet
	dataIn = system.dataset.toPyDataSet(datasetIn)

	headers = list(dataIn.getColumnNames())
	dataOut = [row for row in dataIn if row[colName] == value]

	return system.dataset.toDataSet(headers, dataOut)

findData = findEquals(dataSet, 'letter', 'B')

result:

row | letter | number
---------------------
0   | B      | 2     
1   | B      | 4     
2   | B      | 7     
3   | B      | 11 
2 Likes

Hi @JordanCClark I tested your generic system on a table component, to later use it in my application. but I think I did something wrong. I put the script in propertyChange of a table component. In the end I added the table’s data property to the findData variable and some errors appeared. I will attach them.

Use the script console (under Tools) to test with. I copy/pasted @JordanCClark code and it worked.

I took the test, but the error persists

What version of Ignition?

Also, copy the “Caused by:” portion of the error and post it here. :slight_smile:

version 7.9.9

`Caused by: java.lang.ClassCastException: org.python.core.PyObjectDerived cannot be cast to org.python.core.PySequence

at com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities.toDataSet(DatasetUtilities.java:487)

at sun.reflect.GeneratedMethodAccessor364.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:186)

... 25 more

Traceback (most recent call last):
File “”, line 30, in
File “”, line 28, in findEquals
at com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities.toDataSet(DatasetUtilities.java:487)

at sun.reflect.GeneratedMethodAccessor364.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

java.lang.ClassCastException: java.lang.ClassCastException: org.python.core.PyObjectDerived cannot be cast to org.python.core.PySequence
`

Ah, makes sense, then. Written for 8.0+

in the function try these two lines instead:

	headers = list(datasetIn.getColumnNames())
	dataOut = [list(row) for row in dataIn if row[colName] == value]
1 Like

the error was solved, but it doesn’t give me any results. It’s normal?

Well, it gave you a dataset. Now, you have to do something with it. :wink:

1 Like

It worked here. Thank you so much for all your support.

2 Likes

Hi Jordan,

I tried your script and it worked flawlessly in the script console. When I move it to a gateway script, it doesn't run. Any idea what could be the issue? TIA.

Well, that narrows it down to just about everything.

  • Is there an error in the log?
  • How are you trying to use it?
  • Any sample data you're willing to share, with expected results?
4 Likes

I am trying to use it in a timer gateway script to look at different clients and count how many times a user is logged in at on a specific PC. It is a timer script.

From Error Log:


com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "", line 31, in File "", line 24, in findEquals NameError: global name 'system' is not defined

Caused by: org.python.core.PyException: NameError: global name 'system' is not defined

Example code:


import system
from java.lang import System

sessions = system.util.getSessionInfo()
def findEquals(datasetIn, colName,value,colName2,value2):
	# Convert to PyDataSet
	dataIn = system.dataset.toPyDataSet(sessions)
	import system
	headers = list(dataIn.getColumnNames())
	dataOut = [row for row in dataIn if (row[colName] == value and row[colName2] == value2)]


	return system.dataset.toDataSet(headers, dataOut)
findData = findEquals(sessions, 'username', 'Lockout','address', 'DESKTOP-XXXXXX')
rowCount = findData.getRowCount()

if(rowCount > 0):
	system.tag.writeBlocking(['[default]LockoutUserRole'], True)

Gateway event scope bamboozlery.
Don't write scripts in the gateway event, put them in the script library and call them from the event.
Don't import system.

3 Likes

Without line numbers to back me up, this block seems like the culprit as you're trying to import system AFTER you've already tried to use it.

1 Like

Thanks for your reply. I had the import outside the definition at first, without it inside the definition. Scrambling to try and make it work I thought to actually add it to the definition, as well as "sessions" but copied it to the wrong spot. Stupid error on my part .. its working now. Thank you.