Not Iterating Through Dataset Correctly

Hi All,

I am trying to have Ignition return the value of the first non-blank cell in a column. I have the dataset being pulled from a table that is sorted in descending order by Timestamp. The idea is I want to get the most recent non-blank value for a column.

I am running into issues where the value that is pulled is not the most recent one, but is one further down the table. I have it recreate the dataset with every button press to ensure it’s the most recent. Does Ignition store old table configurations as the dataset and not allow it to be overwritten with the most current values?

Here is the code snippet that I am using. In theory it should be pretty basic, is there something I am missing?

Any help would be appreciated!

hey! is your dataset (.data) actually sorted, or is the just view/visualization of the data sorted?

also looks like you might need to break your “for” loop after you find what you’re looking for i.e.

for row in data:
    if row['swMajorB1'] != "":
        swMajorB1Current = row['swMajorB1']
        break

if you are trying to return it to a custom property, you’ll probably have to do something like:

event.source.myCustomProp = swMajorB1Current
1 Like

Hi Paul,

Thanks for the response. You were right in that it appears the dataset wasn’t sorting, but just the table that I was able to see. Thanks for the help!

Related question:
I want to be able to move across the selected row to pull values in other columns. For example, if the row in the above code ended up having an index of 3, can I call on that row index to reference cells in other columns in that same row?

Yes.
{Noise to hit twenty characters.}

Would you mind telling me how? I’m new to this so am still learning this code that might seem very obvious to others.

Use the dataset’s .getValueAt() method, documented here.

Thanks for the reply, that gets me almost all of the way there. The missing piece I have is I’m trying to return the value of the index for the code above. I imagine it would be something like:

rowIndex = index.row[“swMajorBl”]

Is there a way to get rowIndex to return an integer value for the row[“swMajorBl”] once it completes the For loop?

From there I’d use the .getValueAt() to obtain the cell value.

for i in range(data):
    if data[i]['swMajorB1'] != "":
        swMajorB1Current = data[i]['swMajorB1']
        return desired_value = data.getValueAt(i, column_index_or_name)
return None

This will only get you the first instance of a dataset row value which is not an empty String. It sounds like you have a complicated use-case, so I’m not sure exactly what you’re looking to accomplish.

Hi cmallonee,

This might be a better piece of code to use as an example:

Once the For loop finds the matching row value in Line 108, I’d like to use that row index to move across the dataset and get the values for other columns in that same row. From what you and pturmel mentioned, it looks like the .getValueAt() is what I need to use.

It looks like I need to use [ i ] instead of row to be able to call the index, is that correct?

You have the row object. You can reference any of its columns there. If you need to put the row number away to use after the loop completes, you will need to track that in the loop. I prefer the enumerate() function for such things. Like so:

for i, row in enumerate(ecuData):
    # you have both row index and row here.
2 Likes

It’s much easier that way yes. I only use for instance_of in some_list: when I’m doing something for every instance in the collection. If you need to apply selective logic, then it’s usually easier to use index referencing.

If you need the multiple pieces of the row, you could just return the row and then pull what you need from it:

desired_row= None
for row in ecuData:
    if row['ecuName'] = some_value:
        desired_row = row
if desired_row:  #the implication here is that it is no longer None
    x = desired_row['x']
    y = desired_row['y']

Or you could return the values from within the for loop:

desired_row= None
for row in ecuData:
    if row['ecuName'] = some_value:
        x = desired_row['x']
        y = desired_row['y']
        return x, y . # would return as a tuple

If you need values from a DIFFERENT row, or you need to refer to a column by index, then that would be the time to use index referencing within the for loop.

for i in range(data):
    if data[i]['swMajorB1'] != "":
        swMajorB1Current = data[i]['swMajorB1']
        return desired_value = data.getValueAt(i, column_index_or_name)
1 Like

Perfect! The pull out the selected row method worked best for me. Thank you both for the help!