# Expression "lookup" logic function

**URL:** <https://forum.inductiveautomation.com/t/expression-lookup-logic-function/2050>\
**Category:** Ignition\
**Created:** [July 18, 2010, 11:08am UTC](https://forum.inductiveautomation.com/t/expression-lookup-logic-function/2050 "2010-07-18T11:08:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@nathan](https://forum.inductiveautomation.com/u/nathan)\
**Post date:** [July 18, 2010, 11:08am UTC](https://forum.inductiveautomation.com/t/expression-lookup-logic-function/2050/1 "2010-07-18T11:08:05Z")

</div>

Using an expression like the minute example below, consider expecting: 0-15 returns 1, 16-30 returns 2, 31-45 returns 3, and 46-59 returns 4. It’d be much nicer to reference the variable once like with a switch, than using many nested ifs.

```auto
dateExtract({[System]Client/System/CurrentDateTime}, "minute")
```

MS Excel uses LOOKUP. For example:

`=LOOKUP(45,{0,60,63,67,70,73,77,80,83,87,90,93,97},{"F","D-","D","D+","C-","C","C+","B-","B","B+","A-","A","A+"}) Assigns (F) 
=LOOKUP(90,{0,60,63,67,70,73,77,80,83,87,90,93,97},{"F","D-","D","D+","C-","C","C+","B-","B","B+","A-","A","A+"}) Assigns (A-) 
=LOOKUP(78,{0,60,63,67,70,73,77,80,83,87,90,93,97},{"F","D-","D","D+","C-","C","C+","B-","B","B+","A-","A","A+"}) Assigns (C+) `

---

<div class="post-metadata">

**Author:** ![Carl.Gould](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/carl.gould/32/7262_2.png) [@Carl.Gould](https://forum.inductiveautomation.com/u/Carl.Gould)\
**Post date:** [July 19, 2010, 7:48pm UTC](https://forum.inductiveautomation.com/t/expression-lookup-logic-function/2050/2 "2010-07-19T19:48:07Z")

</div>

We have a lookup expression that you could use - but you’d have to put the values in a dataset.

Or, for your example, you simply use this expression:

```auto
toInt(floor(max(0,dateExtract(now(), "minute")-1)/15))+1
```

---

<div class="post-metadata">

**Author:** ![nathan](https://avatars.discourse-cdn.com/v4/letter/n/c5a1d2/32.png) [@nathan](https://forum.inductiveautomation.com/u/nathan)\
**Post date:** [July 19, 2010, 9:29pm UTC](https://forum.inductiveautomation.com/t/expression-lookup-logic-function/2050/3 "2010-07-19T21:29:36Z")

</div>

The dataset lookup function works like a switch where it needs to find an exact match. What I’m looking for is a function that takes one input and compares it against an ordered range of numbers, like you might do with nested if statements. Imagine x is a long tag path or property.

```auto
if (x >= 100, 5, if (x >= 92, 4, if (x >= 80, 3, if (x >= 40, 2,1))))
```

or equivalently (you might support datasets or dataset columns for the 2 lists)

```auto
lookup(x, {100, 92, 80, 40}, {5, 4, 3, 2}, 1)
```

x=105 returns 5  
x=93 returns 4  
x=80 returns 3  
x=25 returns 1

---

<div class="post-metadata">

**Author:** ![Robert.McKenzie](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/robert.mckenzie/32/4134_2.png) [@Robert.McKenzie](https://forum.inductiveautomation.com/u/Robert.McKenzie)\
**Post date:** [July 19, 2010, 10:18pm UTC](https://forum.inductiveautomation.com/t/expression-lookup-logic-function/2050/4 "2010-07-19T22:18:03Z")

</div>

You could make your own function![code]def ranges(x, inputs, outputs, failover):  
#error checking  
if len(inputs) != len(outputs):  
system.gui.warning(“Lists must be the same length”)  
return failover

```
#loop through the inputs 
for i in range(len(inputs)):
	if x >= inputs[i]:
		return outputs[i]
return failover[/code]And call it with[code]runScript("app.abc.lookup(105, [100,92,80,40], [5,4,3,2], 1)")[/code]

```

Edit: woops, expression… Right: changed.
