unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* How to read from a file
@ 2010-06-18 14:31 Cecil Westerhof
  2010-06-18 15:50 ` Mike Gran
  2010-06-18 16:21 ` Cecil Westerhof
  0 siblings, 2 replies; 3+ messages in thread
From: Cecil Westerhof @ 2010-06-18 14:31 UTC (permalink / raw)
  To: guile-user

When looking at:
    http://www.gnu.org/software/guile/docs/faq/OLD-guile-faq.html

The following code should work:
    #!/usr/bin/guile \
    -e main -s
    !#
    (use-modules (ice-9 readline))
    (activate-readline)

    (define (main args)
      (let ((input-file  (cadr args))
            (output-file (caddr args)))
        (with-input-from-file input-file
          (lambda ()
            (while (not (eof-object? (peek-char)))
                   (display (read-line))
                   (newline))))))

But when executing it, I get:
    ERROR: Unbound variable: read-line

What am I doing wrong?

Also, it says:
    guile's IO performance is not very fast at the moment, so if you
    have to process large files, you may want to use a different model

What would be a more efficient way to process a file?

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to read from a file
  2010-06-18 14:31 How to read from a file Cecil Westerhof
@ 2010-06-18 15:50 ` Mike Gran
  2010-06-18 16:21 ` Cecil Westerhof
  1 sibling, 0 replies; 3+ messages in thread
From: Mike Gran @ 2010-06-18 15:50 UTC (permalink / raw)
  To: Cecil Westerhof, guile-user

> From: Cecil Westerhof Cecil@decebal.nl

What am I doing wrong?

You're confusing (ice-9 readline) with (ice-9 rdelim).
The former adds the ability to edit from the guile prompt.
The latter adds a function to read whole lines from
ports.

I might have written your script this way

(use-modules (ice-9 rdelim))
(define (main args)
  (let ((input-file  (cadr args))
        (output-file (caddr args)))
    (with-input-from-file input-file
      (lambda ()
        (let loop ((line (read-line)))
          (cond ((not (eof-object? line))
                 (display line)
                 (newline)
                 (loop (read-line)))))))))

-Mike



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: How to read from a file
  2010-06-18 14:31 How to read from a file Cecil Westerhof
  2010-06-18 15:50 ` Mike Gran
@ 2010-06-18 16:21 ` Cecil Westerhof
  1 sibling, 0 replies; 3+ messages in thread
From: Cecil Westerhof @ 2010-06-18 16:21 UTC (permalink / raw)
  To: guile-user

Op vrijdag 18 jun 2010 16:31 CEST schreef Cecil Westerhof:

> When looking at:
> http://www.gnu.org/software/guile/docs/faq/OLD-guile-faq.html
>
> The following code should work:
> #!/usr/bin/guile \
> -e main -s
> !#
> (use-modules (ice-9 readline))
> (activate-readline)
>
> (define (main args)
> (let ((input-file  (cadr args))
> (output-file (caddr args)))
> (with-input-from-file input-file
> (lambda ()
> (while (not (eof-object? (peek-char)))
> (display (read-line))
> (newline))))))
>
> But when executing it, I get:
> ERROR: Unbound variable: read-line
>
> What am I doing wrong?

I found it:
    #!/usr/bin/guile \
    -e main -s
    !#
    (use-modules (ice-9 readline))
    (use-modules (ice-9 rdelim))
    (activate-readline)

    (define (main args)
      (let ((input-file-name  (cadr args))
            (output-file-name (caddr args))
            (input-file       "")
            (output-file      "")
            (this-line        ""))
        (set! input-file  (open-file input-file-name  "r"))
        (set! output-file (open-file output-file-name "w"))
        (while (not (eof-object? (peek-char input-file)))
               (set! this-line (read-line input-file))
               (write-line this-line output-file)
               )
        (close-port output-file)
        (close-port input-file)))

A file of 5.000.000 lines and 163 MB is processed in 26 seconds. At the
moment it is just copying, but that is properly the most expensive part.
There should be some error checking and the processing should be done,
but that is the next step.

Could this be done more efficient?

By the way, it is not that bad. My Emacs Lisp code takes about two times
as long. It does also a replace, but that will not take 50% of the time.
So it looks like Guile has a good performance notwithstanding that it is
said that  Guile's IO performance is not very fast at the moment. It is
about as fast as a Perl script. The problem with the Perl script is that
it only works with ASCII. It goes wrong on files that contain other
characters. That is why I at the moment use the Emacs Lisp script. I am
wondering what will happen when Guile's IO performance becomes good.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-06-18 16:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-06-18 14:31 How to read from a file Cecil Westerhof
2010-06-18 15:50 ` Mike Gran
2010-06-18 16:21 ` Cecil Westerhof

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).