Find / find-all

Started by newdep, April 05, 2008, 03:46:40 AM

Previous topic - Next topic

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

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

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

#3
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">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

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


(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

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