function for greatest object in list less than or equal to a value

Dr. Hawkins dochawk at gmail.com
Wed Oct 14 14:21:36 EDT 2015


On Mon, Oct 12, 2015 at 4:24 PM, Geoff Canyon <gcanyon at gmail.com> wrote:

> function greatestLessThan pList,V
>    sort items of pList descending numeric
>    sort items of pList by each >= V
>    return item 1 of pList
> end greatestLessThan


Something along these lines was what I'm trying to wrap my head around,
when it twisted my mind into a pretzel.

I came up with

*on* mouseUp

   *put* getNxtSmlst("2, 5, 8, 15, 22", 8)

*end* mouseUp


*function* getNxtSmlst list,floorVal



   *sort* items of list by cmpr(each,floorVal)




*end* getNxtSmlst


*function* cmpr val,flr



   *if* val < flr *then*

      *return* val -flr

   *else*

      *return* -1

   *end* *if*



*end* cmpr


But staying with pure intrinsics should be a wind, I'd think.

That one came from *wanting * to write,


sort items of theList descending by (each <= floor) * (floor-each)


--but aside from liveCode not coercing the logical to a 1/0 value (as I
thought it did--too many languages over the years!), I think it misses the
case where the value is in the list.

This is always going to have a relatively small number of entries in the
list; it would be more than unusual to have more than a couple of dozen
transitions between the "canned" paragraph text and the values merged in.
The function will be called on mouseMove, though, to pop up an field over
the merge values as the mouse passes over them.

I know that the elements of an array aren't in any particular order, but
does keys return them in numeric order if they are all numeric?  (Some
distant memory says that this is an exception.  The keys also would have
been added in numeric order.  For that matter, they could be stored on
render.  In any event, I think the algorithm can presume that the numbers
are ordered.

And I'm amazed at how much more I'm learning about other pieces of liveCode.


-- 
Dr. Richard E. Hawkins, Esq.
(702) 508-8462



More information about the use-livecode mailing list