Page 1 of 1

[proposal] We have "format"... could we have "scan" ?

Posted: Thu Nov 28, 2013 4:38 pm
by Ormente
Hi Lutz,

Sometimes REGEX are too slow for simple things that could be better done with the scan, the "inverse" of format.

Do you think it could be possible to have a scan function in newlisp, or have you an alternative to suggest ?

Thanks

Re: [proposal] We have "format"... could we have "scan" ?

Posted: Thu Nov 28, 2013 7:19 pm
by Lutz
You could do something like this:

Code: Select all

(define (scan fmt txt vars)
    (map set vars (map apply fmt (map list (parse txt))))
)

> (scan '(string float int) "hello 123.45 789" '(str fval ival))
("hello" 123.45 789)
> str
"hello"
> fval
123.45
> ival
789
> 
or this variation:

Code: Select all

(define (scan fmt txt)
    (map set (args) (map eval (map list fmt (parse txt))))
)

(scan '(string float int) "hello 123.45 789" 'str 'fval 'ival)