PerduGames
PerduGames

Reputation: 1228

How to do auto input in multiple read-lines?

How to do auto input in multiple read-line?

(let ((out (with-output-to-string (*standard-output*) 
                (let ((*standard-input* (make-string-input-stream "y y")))
                  (when (find (read-line) '("yes" "y" "t") :test #'string-equal)
                    (print "aaaaa"))
                  (when (find (read-line) '("yes" "y" "t") :test #'string-equal)
                    (print "bbbbbb"))
                  ))))
     out)

I try like this, and I get:

; Evaluation aborted on #<END-OF-FILE {10048FD503}>.

This code work with read, but I need with read-line.

Upvotes: 0

Views: 265

Answers (3)

Rainer Joswig
Rainer Joswig

Reputation: 139331

The without-to-string is unnecessary for an example...

CL-USER 177 > (flet ((yes-p (input-string &aux (yes-words '("yes" "y" "t")))
                       "returns T when the input-string is one of yes, y or t."
                       (find input-string yes-words :test #'string-equal)))
                (with-input-from-string (*standard-input* (format nil "y~%y"))
                  (when (yes-p (read-line))
                    (print "aaaaa"))
                  (when (yes-p (read-line))
                    (print "bbbbbb"))
                  (values)))

"aaaaa" 
"bbbbbb" 

Upvotes: 0

PerduGames
PerduGames

Reputation: 1228

I made it work like this:

(with-output-to-string (*standard-output*)
  (with-input-from-string (*standard-input* (format nil "y~%y"))
    (when (find (read-line) '("yes" "y" "t") :test #'string-equal)
      (print "aaaaa"))
    (when (find (read-line) '("yes" "y" "t") :test #'string-equal)
      (print "bbbbbb"))))

Upvotes: 1

Renzo
Renzo

Reputation: 27424

Another possibility is to use the parameter of read-line that requires to return nil on end of file:

(let ((out (with-output-to-string (*standard-output*) 
        (let ((*standard-input* (make-string-input-stream "y y")))
          (when (find (read-line *standard-input* nil) '("yes" "y" "t") :test #'string-equal)
             (print "aaaaa"))
          (when (find (read-line *standard-input* nil) '("yes" "y" "t") :test #'string-equal)
             (print "bbbbbb"))))))
  out)

Upvotes: 2

Related Questions