newLISP Fan Club

Forum => Anything else we might add? => Topic started by: cormullion on March 06, 2006, 02:27:41 AM

Title: ref returns list of indexes?
Post by: cormullion on March 06, 2006, 02:27:41 AM
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?
Title:
Post by: newdep on March 06, 2006, 04:01:59 AM
'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.
Title:
Post by: cormullion on March 06, 2006, 04:25:01 AM
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?
Title:
Post by: newdep on March 06, 2006, 04:42:11 AM
yes its like that...
Title:
Post by: Lutz on March 06, 2006, 07:32:17 AM
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
Title:
Post by: cormullion on March 06, 2006, 08:15:29 AM
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 ;-)
Title:
Post by: nigelbrown on March 06, 2006, 12:05:08 PM
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