Page 1 of 1

ref returns list of indexes?

Posted: Mon Mar 06, 2006 10:27 am
by cormullion
Please correct my faulty understanding of ref:

Code: Select all

(set 'data '("a" "b" "c" "d" "e" "f" "g" "a" "b" "c" "d" "e"))
(ref "b" data)
I'm expecting this to return (1 8), yet it returns (1). Why is this?

Posted: Mon Mar 06, 2006 12:01 pm
by newdep
'ref is not repeating and find first occeurens found also nested lists..

so if you like to search multple strings in your list you could use 'map or

>(index (lambda (x) (= "b" x)) '("a" "b" "c" "d" "e" "b"))
(1 5)
-- is not seeking in nested lists --



there are even more options..


Regards,
Norman.

Posted: Mon Mar 06, 2006 12:25 pm
by cormullion
Thanks - the great pleasure of newLISP is that there's always a more elegant solution that I hadn't thought of! ;-)

I was reading the manual and got the wrong idea:
ref searches for expression exp in list and returns a list of integer indexes or an empty list if the exp cannot be found.
but in fact it only finds the 'hierachy level' that it finds the exp in?

Posted: Mon Mar 06, 2006 12:42 pm
by newdep
yes its like that...

Posted: Mon Mar 06, 2006 3:32 pm
by Lutz
It says "list of indexes" because 'ref' is mostly used for multidimensional searches:

Code: Select all

(set 'L '(a b (c d e) f g))

(ref 'd L) => (2 1)

Lutz

Posted: Mon Mar 06, 2006 4:15 pm
by cormullion
How about:

ref searches for expression exp in list and returns a list of integer indexes sufficient to identify the locaion of the first occurrence in a multi-dimensional list. or an empty list if the exp cannot be found....

or something ;-)

Posted: Mon Mar 06, 2006 8:05 pm
by nigelbrown
Yes, it is clearer to specifiy it finds only the first.
It may also be worth specifying the search is depth-first

viz
> (ref 'b '(a b (b c) b))
(1)
> (ref 'b '(a (b c) b))
(1 0)
>

Nigel