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

For the Compleat Fan
Locked
Ormente
Posts: 23
Joined: Tue Aug 31, 2010 1:54 pm
Location: Near Mâcon, France

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

Post 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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

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

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

Locked