[racket] Use regexps or something else?

From: Rodolfo Carvalho (rhcarvalho at gmail.com)
Date: Sat Jun 4 18:48:47 EDT 2011

On Sat, Jun 4, 2011 at 12:50, Eli Barzilay <eli at barzilay.org> wrote:

> 11 hours ago, Rodolfo Carvalho wrote:
> >
> > Imagine an scenario that instead of 2 (as above) we had 20 columns
> > of a table, and we wanted to return a table of the squares of the
> > values for all columns, except for columns 12 and 15 (of course we
> > could parse them out and ignore then when processing/returning, but
> > you get the idea...)
>
> A classical mistake that people do when they start playing with
> regexps is they forget about the rest of the language and try to do
> too much with regexps.  If you have a table with more than 2 numbers,
> I'd do it like this:
>
>  (define t "
>    23 12 72367 2772 7272 7777 2.342
>    15 45 1e2 1e3 1e4
>    32 27 3.1415926")
>
>  (map (λ(l) (map string->number (regexp-match* #px"\\d+" l)))
>       (regexp-split #rx"\n" t))
>


This fails to read "2.342", "1e4" ...


Following your advice and the piece of code above, I wrote it like this:

;; Split lines, match numbers and filter-out invalid lines
(define parsed-data
  (filter
   (λ (l) (> (length l) 1))
   (map (λ (l)
          (map string->number (regexp-match* #px"\\b[\\d.e+]+\\b" l)))
        (regexp-split #rx"\n" t))))

;; Sort by Gflops, convert back to string, attach header and display
(displayln (string-join
            (cons
             "N NB P Q Time Gflops"
             (map (λ (row) (string-join (map number->string row) "\t"))
                  (sort parsed-data
                        >
                        #:key last
                        #:cache-keys? #t)))
            "\n"))


[]'s

Rodolfo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20110604/197cc74d/attachment.html>

Posted on the users mailing list.