| Re: search routine [message #54384 is a reply to message #54247] |
Fri, 01 June 2007 07:58  |
cmancone
Messages: 30 Registered: May 2007
|
Member |
|
|
On Jun 1, 10:27 am, Paolo Grigis <pgri...@astro.phys.ethz.ch> wrote:
> Laurens wrote:
>> Paolo Grigis wrote:
>
>>> Laurens wrote:
>>>> Hi folks,
>
>>>> From Martin Schultz (posted in 1999) I found the following
>>>> array-search algorithm which seems to do a fine job.
>>>> Except that i'm not able to catch the first element in the array.
>
>>>> Example:
>
>>>> Array = [0,80,100,120,180,300]
>>>> result = search, Array, 4.53
>
>>>> It should return index 0, if I understand it correctly, but it
>>>> returns 1 instead. Now I don't quite follow the logic of the
>>>> function, so maybe someone for which it's easy to see can help me in
>>>> the right direction?
>
>>> You could use the built-in function value_locate instead:
>
>>> result=value_locate(array,4.53)
>
>>> which returns 0.
>
>>> Ciao,
>>> Paolo
>
>> though i'm noticing that it takes its ground number to be returned.
>> If my array has [0,10,20,30] and i'm searching for 18, it will return
>> 10. Now its just that would want to get 20 instead of 10 :)
>
> well, in that case, just increase the index of the returned
> value by one (though you'd better check that it wasn't the
> *last* element...).
>
> Ciao,
> Paolo
>
>
>
>> regards, Laurens
Just to make sure it's clear, built in functions (value_locate) are
always better - I was just pondering how it might be done if there
wasn't a built in function. It's always fun to work out a problem
anyway, even if there's already a correct answer.
|
|
|
|