This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

text munging


>>>>> "Gregg" == Gregg Reynolds <greynolds@enteract.com> writes:

Gregg> Usually I use perl to rectify the patterned errors I find, but
Gregg> I'd like to use Guile.  Unfortunately I don't at the moment
Gregg> have time to experiment much, so I was wondering if somebody on
Gregg> the list could tell me what Guile's answer to "perl -np foo.pl
Gregg> input.txt" is.  The "input.txt" file just contains a bunch of
Gregg> search and replace expressions like "s/foo/bar/g;".  

Gregg> Really simple and easy, especially since it allows me to break
Gregg> problems into parts and apply a sequence of transformations,
Gregg> one per line; this aspect of perl syntax has high nift.  If
Gregg> Guile is going to compete with the likes of perl, it would be
Gregg> Goodness Itself to be able to do such things with similar ease
Gregg> and smugness.  How would the guile meisters do this?  (Think
Gregg> "cookbook".)

Well, I dunno how much people are going to like it, but here's a
scheme hack I threw together to do it :)  Hardly efficient, but kinda
neat, and not bad for an hours hacking.

the basic code is in "mungeline.scm" to use it, call it with 

guile -s mungeline.scm foo.scm input.txt

Here's a sample foo.scm, that first replaces all foo's with bar's,
replaces baz with "look ma, it's backwards!" and swaps the text before 
and after the baz (for every occurance of baz in the line, which will
be confusing, but hell :), then truncates any line where EOL appears,
then downcases any capital letters, and finally, take any word
surrounded by -- and strip off the --'s.

("foo" 'pre "bar" 'post)
("baz" 'post "look ma, it's backwards!" 'pre)
("EOL" 'pre)
("[A-Z]+" 'pre
          (lambda (match) (string-downcase (match:substring match)))
          'post)
("--([a-zA-Z]+)--" 'pre 1 'post)

here's the source to mungeline.scm:
--------------------------------------------------------------------------------
(use-modules (ice-9 streams)
             (ice-9 regex))

(define str (port->stream (open-file (caddr (command-line)) "r") read-line))

(define (make-transformer reg l f)
  (lambda (line)
    (apply regexp-substitute/global
           (append (list #f reg (f line)) (map eval l)))))

(define transformer (stream-fold
                     (lambda (l proc)
                       (make-transformer (make-regexp (car l)) (cdr l) proc))
                     (lambda (x) x)
                     (port->stream (open-file (cadr (command-line)) "r") read)))

(define (do-thing)
  (stream-for-each
   (lambda (x)
     (display (transformer x))
     (newline))
   str))

(do-thing)

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]