ref returns list of indexes?

Started by cormullion, March 06, 2006, 02:27:41 AM

Previous topic - Next topic

cormullion

Please correct my faulty understanding of ref:


(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?

newdep

#1
'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.
-- (define? (Cornflakes))

cormullion

#2
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:


Quoteref 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?

newdep

#3
yes its like that...
-- (define? (Cornflakes))

Lutz

#4
It says "list of indexes" because 'ref' is mostly used for multidimensional searches:



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

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




Lutz

cormullion

#5
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 ;-)

nigelbrown

#6
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