newLISP Fan Club

Forum => newLISP in the real world => Topic started by: newdep on April 05, 2008, 03:46:40 AM

Title: Find / find-all
Post by: newdep on April 05, 2008, 03:46:40 AM
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..
Title:
Post by: newdep on April 05, 2008, 12:34:16 PM
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)
Title:
Post by: cormullion on April 05, 2008, 01:58:43 PM
(set 'key "newlisp")
(set 'file (open "/usr/bin/newlisp" "read"))

(while (search file key)
   (println (read-line file)))


is a bit quicker, I think.
Title:
Post by: newdep on April 05, 2008, 02:18:18 PM
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"..
Title:
Post by: cormullion on April 05, 2008, 02:50:12 PM
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!
Title:
Post by: newdep on April 05, 2008, 03:01:48 PM
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)