LISP文件读取循环条件和序号

时间:2017-06-06 04:19:40

标签: loops lisp common-lisp conditional-statements file-read

您好我对文件读取中的LISP LOOP条件有疑问 以及关于循环中的序号

我想要做的是读取文件并保存数据(我简单地实现了它)

但要做到这一点,我不知道如何完成循环。

我只想在文件结束时完成循环。

我希望在循环中使用序号。

例如,我想每次在循环周期中保存struct s1,s2,s3,s4 .....中的数据,但是怎么做?

它只是用C语言伪代码

表达
int i=0;
while( != EOF){

    read file in line
    save data into struct[i]
    i++
}

我的代码就是这个

(loop 
    ??(setf p(n) (make-player
        :name (read-line *file nil)
        :team (read-line *file nil)
        :game-number (read-line *file nil)
    )
    ??(setf (gethash (player-name p(n) Player_DB) p(n))

    ??(when ( = (read-line *file) " ") (return 0))  
)   

1 个答案:

答案 0 :(得分:2)

您有两种选择:

带有read-line

它返回nil,所以你可以停止那个或者指定一个值,当行为零时停止它

作为一个例子:

  • 停止使用自定义符号

    (with-open-file (stream "/Users/toni/learn/lisp/cl-l/stackoverflow/scripts/list-file.txt")
      (loop for line = (read-line stream nil 'foo)
            until (eq line 'foo)
            do (print line)))
    

    结果:

    "-rw-r--r--  1 toni  staff     22 18 abr 09:34 calimero_1.txt" 
    "-rw-r--r--  1 toni  staff      1 18 abr 09:34 calimero_2.txt" 
    "-rw-r--r--  1 toni  staff      0 18 abr 09:34 calimero_3.txt" 
    "-rw-r--r--  1 toni  staff    149 18 abr 09:34 charpos.lisp" 
    "-rw-r--r--  1 toni  staff    485  3 may 16:14 distributive-lists.org" 
    "-rw-r--r--  1 toni  staff   1237 18 abr 09:34 do-macro.lisp" 
    "-rw-r--r--  1 toni  staff    120 18 abr 09:34 download-all-pdfs.lisp" 
    "-rw-r--r--  1 toni  staff   3111 18 abr 09:34 emacs-rest-client" 
    "-rw-r--r--  1 toni  staff   1111 30 may 15:46 equal-function-in-common-lisp.org" 
    "-rw-r--r--  1 toni  staff     12 18 abr 09:34 filename.txt" 
    "-rw-r--r--  1 toni  staff    757 18 abr 09:34 find-all-objects-in-apackage.lisp" 
    "-rw-r--r--  1 toni  staff    355 18 abr 09:34 flatten-one-level.lisp" 
    "-rw-r--r--  1 toni  staff    813 18 abr 09:34 format-money.lisp" 
    "-rw-r--r--  1 toni  staff   2137 18 abr 09:34 hash-table-eficiency.lisp" 
    "-rw-r--r--  1 toni  staff   4230 18 abr 09:34 instrospection.lisp" 
    "-rw-r--r--  1 toni  staff    919 18 abr 09:34 mapcar_and_lambda.lisp" 
    "-rw-r--r--  1 toni  staff  11713 18 abr 09:34 mastermind.lisp" 
    "-rw-r--r--  1 toni  staff    368 18 abr 09:34 method-missing.lisp" 
    "-rw-r--r--  1 toni  staff    614 18 abr 09:34 method_missing.rb" 
    "-rw-r--r--  1 toni  staff      2 18 abr 09:34 patofante_1.txt" 
    "-rw-r--r--  1 toni  staff    314 24 may 18:29 profiling.org" 
    "-rw-r--r--  1 toni  staff   1265 18 abr 09:34 recursion-more-one-function.lisp" 
    "-rw-r--r--  1 toni  staff   1154 18 abr 09:34 size-explosing-file.lisp" 
    "-rw-r--r--  1 toni  staff    592 18 abr 09:34 sorting-coordinates.lisp" 
    "-rw-r--r--  1 toni  staff    485 18 abr 09:34 stackoverflow-mongo.lisp" 
    "-rw-r--r--  1 toni  staff   8174 29 may 14:06 test-with-c-variables.org" 
    "-rw-r--r--  1 toni  staff  11121 25 abr 11:18 test.html" 
    "-rw-r--r--  1 toni  staff    690 25 abr 13:45 test.org" 
    "-rw-r--r--  1 toni  staff    236 18 abr 09:34 text.txt" 
    "-rw-r--r--  1 toni  staff  61194 18 abr 09:34 train.csv" 
    "-rw-r--r--  1 toni  staff    218 26 abr 17:17 variables-package.lisp" 
    "-rw-r--r--  1 toni  staff   1792 18 abr 09:34 vectors-vs-lisp.lisp" 
    "-rw-r--r--  1 toni  staff    190 18 abr 09:34 weird-code.lisp" 
    "-rw-r--r--  1 toni  staff    629 18 abr 09:34 write-a-list.lisp"
    
  • 以nil停止

    (with-open-file (stream "/Users/toni/learn/lisp/cl-l/stackoverflow/scripts/list-file.txt")
      (loop for line = (read-line stream nil)
            until (null line)
            do (print line)))
    

    结果:

    "-rw-r--r--  1 toni  staff     22 18 abr 09:34 calimero_1.txt" 
    "-rw-r--r--  1 toni  staff      1 18 abr 09:34 calimero_2.txt" 
    "-rw-r--r--  1 toni  staff      0 18 abr 09:34 calimero_3.txt" 
    "-rw-r--r--  1 toni  staff    149 18 abr 09:34 charpos.lisp" 
    "-rw-r--r--  1 toni  staff    485  3 may 16:14 distributive-lists.org" 
    "-rw-r--r--  1 toni  staff   1237 18 abr 09:34 do-macro.lisp" 
    "-rw-r--r--  1 toni  staff    120 18 abr 09:34 download-all-pdfs.lisp" 
    "-rw-r--r--  1 toni  staff   3111 18 abr 09:34 emacs-rest-client" 
    "-rw-r--r--  1 toni  staff   1111 30 may 15:46 equal-function-in-common-lisp.org" 
    "-rw-r--r--  1 toni  staff     12 18 abr 09:34 filename.txt" 
    "-rw-r--r--  1 toni  staff    757 18 abr 09:34 find-all-objects-in-apackage.lisp" 
    "-rw-r--r--  1 toni  staff    355 18 abr 09:34 flatten-one-level.lisp" 
    "-rw-r--r--  1 toni  staff    813 18 abr 09:34 format-money.lisp" 
    "-rw-r--r--  1 toni  staff   2137 18 abr 09:34 hash-table-eficiency.lisp" 
    "-rw-r--r--  1 toni  staff   4230 18 abr 09:34 instrospection.lisp" 
    "-rw-r--r--  1 toni  staff    919 18 abr 09:34 mapcar_and_lambda.lisp" 
    "-rw-r--r--  1 toni  staff  11713 18 abr 09:34 mastermind.lisp" 
    "-rw-r--r--  1 toni  staff    368 18 abr 09:34 method-missing.lisp" 
    "-rw-r--r--  1 toni  staff    614 18 abr 09:34 method_missing.rb" 
    "-rw-r--r--  1 toni  staff      2 18 abr 09:34 patofante_1.txt" 
    "-rw-r--r--  1 toni  staff    314 24 may 18:29 profiling.org" 
    "-rw-r--r--  1 toni  staff   1265 18 abr 09:34 recursion-more-one-function.lisp" 
    "-rw-r--r--  1 toni  staff   1154 18 abr 09:34 size-explosing-file.lisp" 
    "-rw-r--r--  1 toni  staff    592 18 abr 09:34 sorting-coordinates.lisp" 
    "-rw-r--r--  1 toni  staff    485 18 abr 09:34 stackoverflow-mongo.lisp" 
    "-rw-r--r--  1 toni  staff   8174 29 may 14:06 test-with-c-variables.org" 
    "-rw-r--r--  1 toni  staff  11121 25 abr 11:18 test.html" 
    "-rw-r--r--  1 toni  staff    690 25 abr 13:45 test.org" 
    "-rw-r--r--  1 toni  staff    236 18 abr 09:34 text.txt" 
    "-rw-r--r--  1 toni  staff  61194 18 abr 09:34 train.csv" 
    "-rw-r--r--  1 toni  staff    218 26 abr 17:17 variables-package.lisp" 
    "-rw-r--r--  1 toni  staff   1792 18 abr 09:34 vectors-vs-lisp.lisp" 
    "-rw-r--r--  1 toni  staff    190 18 abr 09:34 weird-code.lisp" 
    "-rw-r--r--  1 toni  staff    629 18 abr 09:34 write-a-list.lisp"