Find / find-all

Q&A's, tips, howto's
Locked
newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Find / find-all

Post by newdep »

Somethnig I always struggle with but often use is finding strings inside
strings...

a simple 'find' does already return an index, very good!
But a 'find-all' does always return the content..

Is it possible to let 'find-all' return indexes instead of content?


PS: im actually wondering if 'find' should be embedded to find multiple
occurrences or 'find-all' , regarding speed..
-- (define? (Cornflakes))

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Can it be done quicker?


(silent (set 'ibuf (read-file "/usr/bin/newlisp")))
(set 'key "newlisp")

(define (sniff key data, ret (l (length key)))
(for (s 0 (length data))
(and (= (s l data) key) (push s ret -1)))
ret)

(sniff key ibuf)
-- (define? (Cornflakes))

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Code: Select all

(set 'key "newlisp") 
(set 'file (open "/usr/bin/newlisp" "read"))

(while (search file key)
   (println (read-line file)))
is a bit quicker, I think.

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

AAaa my mistake the read-file was just an example to load data...
Thanks anyway for the quick cooperation ;-)


Could also be this ->

(silent (set 'ibuf (read-file "http://www.newlisp.org/downloads/newlisp_manual.html")))
(set 'key "newlisp")

(define (sniff key data, ret (l (length key)))
(for (s 0 (length data))
(and (= (s l data) key) (push s ret -1)))
ret)

(sniff key ibuf)


---
The fact actualy is that i cant find any "charming" find function
that returns me multiple indexes and is fast ;-) So im stuck in "for"..
-- (define? (Cornflakes))

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

OK, I see! Well, this might be quicker:

Code: Select all

(while (set 'temp (find key ibuf))
 (push temp ret -1)
 (set 'ibuf ((+ temp (length key)) ibuf)))
Although this returns a series of offsets, ie not

(221651 221755 221851 222831 229819)

but

(221651 97 89 973 6981)

and you'd also have to add back the length of the key. Which would slow you down again!

newdep
Posts: 2038
Joined: Mon Feb 23, 2004 7:40 pm
Location: Netherlands

Post by newdep »

Yes its funny.. actualy 'find' should have an index pointer, now
you always have to create or own index.. I tried to use copymem too
but then you need double buffer and swifting..

would be nice if this would work, more lispy then using a for loop ->

(find-all "newlisp" "url" (index $0))
>( 234 46346 3466 ...)

but not sure how quick that would be...


(meanwhile ....looping a G major...and with one eye on the newlisp manual)
-- (define? (Cornflakes))

Locked