2011-05-09 147 views
7

如何基于给定的值迭代和打印plist的键?基于价值的plist打印键?

例子:

; plist 
(defun my-list() (list :a "hi" :b "no" :c "go")) 

; from that list i want to iterate and print out keys based on values like: 
for each x in ("hi" "go") print x 

; hoping for: 
ac 

进出口新的口齿不清 - 谢谢你:-)

回答

12

喜欢的东西

(loop for (key value) on my-list by #'cddr 
     when (member value '("hi" "go") :test #'equal) 
     do (princ key)) 

第一行移动在列表模式。

4

你可以使用循环宏:

(loop 
    for (key value . rest) on list 
    by #'cddr 
    when (find value '("foo" "bar") :test #'string=) 
    do (princ key))