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

Started by Ormente, November 28, 2013, 08:38:35 AM

Previous topic - Next topic

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

Lutz

You could do something like this:



(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:



(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)