任務是讀取類似「名稱電話」的N字符串並存儲。然後查找存儲的數據,請求中包含「name」。 我的代碼將名稱和數字存儲在散列表中,但沒有找到任何值。存儲的值使用maphash進行檢查(它顯示所有對鍵值)。通用lisp散列表
函數split-by-one-space就是實用的。
(defparameter data (make-hash-table))
(defun split-by-one-space (string) ; to split string: "aaa bbb" -> (aaa bbb)
(loop for i = 0 then (1+ j)
as j = (position #\Space string :start i)
collect (subseq string i j)
while j))
(dotimes (i (read)) ; input data
(let* ((inp (read-line))
(raw (split-by-one-space inp))
(name (string (car raw)))
(phone (cadr raw)))
(format t "Adding: ~W ~W~%" name phone) ; debug
(setf (gethash name data) phone)))
(maphash #'(lambda (k v) (format t "~a => ~a~%" k v)) data) ; this show all stored data
(loop for line = (read-line *standard-input* nil :eof)
until (or (eq line :eof) (eq line nil))
do
(let ((key (gethash line data))) ; it cannot find anything. Why?
(format t "Searching: ~W~%" line) ; debug
(if (null key)
(format t "Not found~%")
(format t "~A=~A~%" (car key) (cdr key)))))
樣品輸入:
3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
請參閱[在Common Lisp中使用字符串對象作爲散列鍵](http://stackoverflow.com/q/1409069/5747548) – jkiiski