[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: cl-ppcre question

William James

11/18/2015 7:40:00 PM

Madhu wrote:

> | I would like to parse color names in X11's rgb.txt. They look like this:
> |
> | 119 136 153 light slate gray
> | 119 136 153 LightSlateGray
> |
> | When parsing each line, I would like to get the color triplet, and the
> | name. I tried it like this:
> |
> | (let ((color-scanner
> | (cl-ppcre:create-scanner
> | "^ +([0-9]{0,2}) +([0-9]{0,2}) +([0-9]{0,2}) +([ a-zA-Z0-9]+)")))
> | (cl-ppcre:scan-to-strings color-scanner " 1 2 3 foo bar baz "))
> |
> | This gives #("1" "2" "3" "foo bar baz ") for the substrings. How can
> | I get rid of the trailing spaces?
>
> Use the right tool for the job :)
>
> (defun parse-rgb-line (string &key (start 0) end)
> (multiple-value-bind (r endr)
> (parse-integer string :start start :end end :junk-allowed t)
> (multiple-value-bind (g endg)
> (parse-integer string :start (1+ endr) :end end :junk-allowed t)
> (multiple-value-bind (b endb)
> (parse-integer string :start (1+ endg) :end end :junk-allowed t)
> (let ((name-startpos
> (position-if-not (lambda (c) (case c ((#\Tab #\Space) t)))
> string :start (1+ endb))))
> (values (format nil "#~2,'0X~2,'0X~2,'0X" r g b)
> (subseq string name-startpos end)))))))

MatzLisp (Ruby):

" 1 2 3 foo bar baz ".strip.split(/\s+/,4)
==>["1", "2", "3", "foo bar baz"]

--
As Helle "Hamas" Klein, political editor of Sweden's largest newspaper
Aftonbladet, boasts: "If the debate is going to be about whether there are
problems with immigrants, we don't want it". Welcome to Sweden, the country
where the media doesn't even pretend to champion freedom of speech, but openly
brags about censorship.
fjordman.blogspot.ca/2005/05/is-swedish-democracy-collapsing.html
1 Answer

William James

4/3/2016 5:54:00 AM

0

WJ wrote:

> Madhu wrote:
>
> > > I would like to parse color names in X11's rgb.txt. They look like this:
> > >
> > > 119 136 153 light slate gray
> > > 119 136 153 LightSlateGray
> > >
> > > When parsing each line, I would like to get the color triplet, and the
> > > name. I tried it like this:
> > >
> > > (let ((color-scanner
> > > (cl-ppcre:create-scanner
> > > "^ +([0-9]{0,2}) +([0-9]{0,2}) +([0-9]{0,2}) +([ a-zA-Z0-9]+)")))
> > > (cl-ppcre:scan-to-strings color-scanner " 1 2 3 foo bar baz "))
> > >
> > > This gives #("1" "2" "3" "foo bar baz ") for the substrings. How can
> > > I get rid of the trailing spaces?
> >
> > Use the right tool for the job :)
> >
> > (defun parse-rgb-line (string &key (start 0) end)
> > (multiple-value-bind (r endr)
> > (parse-integer string :start start :end end :junk-allowed t)
> > (multiple-value-bind (g endg)
> > (parse-integer string :start (1+ endr) :end end :junk-allowed t)
> > (multiple-value-bind (b endb)
> > (parse-integer string :start (1+ endg) :end end :junk-allowed t)
> > (let ((name-startpos
> > (position-if-not (lambda (c) (case c ((#\Tab #\Space) t)))
> > string :start (1+ endb))))
> > (values (format nil "#~2,'0X~2,'0X~2,'0X" r g b)
> > (subseq string name-startpos end)))))))
>
> MatzLisp (Ruby):
>
> " 1 2 3 foo bar baz ".strip.split(/\s+/,4)
> ==>["1", "2", "3", "foo bar baz"]

OCaml:

Str.bounded_split
(Str.regexp " +")
(String.trim " 1 2 3 foo bar baz ")
4 ;;

===>
["1"; "2"; "3"; "foo bar baz"]

--
[T]he number of Jews killed there was 800,000,000.... [T]he Talmud makes it all
clear by informing us that the blood of the holocausted Jews ran to the sea in
a huge tidal wave that swept boulders in its path and was so deep that it
reached the nostrils of the Romans' horses.
nationalvanguard.org/2014/09/disillusioned-part-1/