# Lookup specific values in dataset in a script

**URL:** <https://forum.inductiveautomation.com/t/lookup-specific-values-in-dataset-in-a-script/10408>\
**Category:** Ignition\
**Created:** [August 27, 2015, 7:56am UTC](https://forum.inductiveautomation.com/t/lookup-specific-values-in-dataset-in-a-script/10408 "2015-08-27T07:56:41Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![flavien](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flavien](https://forum.inductiveautomation.com/u/flavien)\
**Post date:** [August 27, 2015, 7:56am UTC](https://forum.inductiveautomation.com/t/lookup-specific-values-in-dataset-in-a-script/10408/1 "2015-08-27T07:56:41Z")

</div>

Hi,

I want to create a script like the lookup expression function. Is it possible ?

For example, I have a dataset with 3 columns (A, B & C) and I want to know the line number of a specific value in column B. Then, I want to know values in column A & C that correspond to this line number.

Thanks,

---

<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:** [August 27, 2015, 11:34am UTC](https://forum.inductiveautomation.com/t/lookup-specific-values-in-dataset-in-a-script/10408/2 "2015-08-27T11:34:22Z")

</div>

A Window to illustrate:  
[Table Lookup Script\_2015-08-27\_0728.proj](https://us1.discourse-cdn.com/inductiveautomation/original/2X/e/e662a7860b2350c8dd5539a1df16a042d97d72f1.proj) (9.53 KB)

The meat of the code:

[code]lookupIn=event.source.parent.getComponent(‘Lookup’).text  
dataIn=system.dataset.toPyDataSet(event.source.parent.getComponent(‘Table’).data)  
i=0  
foundFlag=0  
for row in dataIn:  
if row[‘B’] == lookupIn:  
event.source.parent.getComponent(‘Index’).text=str(i)  
event.source.parent.getComponent(‘ResultA’).text=str(row[‘A’])  
event.source.parent.getComponent(‘ResultB’).text=str(row[‘B’])  
event.source.parent.getComponent(‘ResultC’).text=str(row[‘C’])  
foundFlag=1  
break  
i+=1

if foundFlag==0:  
event.source.parent.getComponent(‘Index’).text=’–’  
event.source.parent.getComponent(‘ResultA’).text=’–’  
event.source.parent.getComponent(‘ResultB’).text=’–’  
event.source.parent.getComponent(‘ResultC’).text=’–’  
[/code]

---

<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:** [August 27, 2015, 12:06pm UTC](https://forum.inductiveautomation.com/t/lookup-specific-values-in-dataset-in-a-script/10408/3 "2015-08-27T12:06:10Z")

</div>

Using Nick Mudge’s PowerScripting Module (for completeness):

[code]lookupIn=event.source.parent.getComponent(‘Lookup’).text  
dataIn=pa.dataset.toData(event.source.parent.getComponent(‘Table’).data)

lookupRow=dataIn.copy()  
lookupRow.filterRows(lambda row: row[“B”] == lookupIn)

if len(lookupRow)\>0:  
event.source.parent.getComponent(‘Index’).text=str(list(dataIn).index(list(lookupRow)[0]))  
event.source.parent.getComponent(‘ResultA’).text=str(lookupRow[0][‘A’])  
event.source.parent.getComponent(‘ResultB’).text=str(lookupRow[0][‘B’])  
event.source.parent.getComponent(‘ResultC’).text=str(lookupRow[0][‘C’])  
else:  
event.source.parent.getComponent(‘Index’).text=’–’  
event.source.parent.getComponent(‘ResultA’).text=’–’  
event.source.parent.getComponent(‘ResultB’).text=’–’  
event.source.parent.getComponent(‘ResultC’).text=’–’[/code]

---

<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:** [August 27, 2015, 12:07pm UTC](https://forum.inductiveautomation.com/t/lookup-specific-values-in-dataset-in-a-script/10408/4 "2015-08-27T12:07:38Z")

</div>

As a note, both methods will return the first match it finds. All others are ignored.

---

<div class="post-metadata">

**Author:** ![flavien](https://avatars.discourse-cdn.com/v4/letter/f/f475e1/32.png) [@flavien](https://forum.inductiveautomation.com/u/flavien)\
**Post date:** [August 27, 2015, 1:46pm UTC](https://forum.inductiveautomation.com/t/lookup-specific-values-in-dataset-in-a-script/10408/5 "2015-08-27T13:46:29Z")

</div>

It works !

I have created a shared function whith your code :

[code]  
def lookup(valueIn,tableIn,colnumber,colresult):  
tableIn=system.dataset.toPyDataSet(tableIn)  
i=0  
foundFlag=0  
for row in tableIn:  
if row[colnumber] == valueIn:  
return str(row[colresult])  
foundFlag=1  
break  
i+=1

```
if foundFlag==0:
  return "No Match"[/code]

```

Thank you
