# Selecting a table row within range of a value

Hello. A bit of logic which is confounding me.

I have a table of, essentially, segments which I’d like to be able to select on the basis of whether an ‘index’ value is within the range of. So by way of example:

my index channel will go from 0 - 1440.

My table dat has entries like so:

``````index    name
0    section1
300    section2
340    section3
634    section4
1067    section5
1399    section6
``````

What’s the best way of having the right table row selected when my index channel is within the range? (e.g. if index >=340 and <634 then section 3 would be active etc).

At the moment I’ve got a CHOPexecute running through the entire table every time it changes and evaluating each row of the table, but I can’t shake the feeling that there must be a neater way.

The only other thought I had was to convert the first column table values to a multi-sample CHOP but then I still can’t work out how to ‘look up’ which sample of the channel the value would correspond to.

You can do it with a Script DAT (see OP Snippets)

or the Select DAT can help you get the table in such a way that the first row is the one that you want. There’s no good example but you can select rows based on a condition (value in first cell converted to integer is greater that some integer you test against). Select Rows par i set to By Condition, though the syntax may be challenging. see wiki

I’m not sure about the implementation that the selectDAT for example uses, but when you have a sorted list, you can use a nice binary search to find it, resulting in an extremly good scaling script:

``````def find( operator, target_index):
search_index = target_index
found_index = 0
indizes = [ float(row[0].val) for row in operator.rows()[1:] ]
while indizes:
found_index = int(len(indizes) / 2)
if indizes[found_index] <= search_index: indizes = indizes[found_index:]
elif indizes[found_index] > search_index: indizes = indizes[:found_index]
if len(indizes) == 1:
return int(indizes[0])

``````

This implementation takes some assumptions, but it works

If you are using the Select DAT, you put this in the Row Select Condition parameter:

float(me.inputCell.val) > op(‘now’)[0]

With From Column set to the column index where your times are. And that’s assuming your rows are sorted, which you can do with the Sort DAT.

1 Like

that’s very clever actually, and has hugely just helped in my understanding of Python syntax (AS3 background!). Thanks.