使用迴圈迭代雜湊表的條目
該迴圈巨集的鍵,值,或一個雜湊表的鍵和值支援迭代。以下示例顯示了可能性,但完整迴圈語法允許更多組合和變體。
超過鍵和值
(let ((ht (make-hash-table)))
(setf (gethash 'a ht) 1
(gethash 'b ht) 2)
(loop for k being each hash-key of ht
using (hash-value v)
collect (cons k v)))
;;=> ((A . 1) (B . 2))
(let ((ht (make-hash-table)))
(setf (gethash 'a ht) 1
(gethash 'b ht) 2)
(loop for v being each hash-value of ht
using (hash-key k)
collect (cons k v)))
;;=> ((A . 1) (B . 2))
過鍵
(let ((ht (make-hash-table)))
(setf (gethash 'a ht) 1
(gethash 'b ht) 2)
(loop for k being each hash-key of ht
collect k))
;;=> (A B)
超值
(let ((ht (make-hash-table)))
(setf (gethash 'a ht) 1
(gethash 'b ht) 2)
(loop for v being each hash-value of ht
collect v))
;;=> (1 2)