就像这样,罗伊:
CSV文件:
- idx,name,phone,address
- 1,"Alan",7676,"Washington DC"
- 2,"Brian",2424,"Boston"
- 3,"Chad",3232,"NY"
矩阵关联列表:
- (setq L
- '(
- (idx name phone address)
- (1 "Alan" 7676 "Washington DC")
- (2 "Brian" 2424 "Boston")
- (3 "Chad" 3232 "NY")
- )
- )
一些搜索功能如下:
- ; _$ (FindItem 'name "John" L) -> nil
- ; _$ (FindItem 'name "Brian" L) -> (2 "Brian" 2424 "Boston")
- ; _$ (FindItem 'phone 3232 L) -> (3 "Chad" 3232 "NY")
- (defun FindItem ( key val L / aL n )
- (setq aL (apply 'mapcar (cons 'list L)))
- (if (setq n (vl-position val (assoc key aL)))
- (mapcar (function (lambda (x) (nth n x))) aL)
- )
- )
编辑:
我同意它比经典(cdr(assoc…)慢这是罗伊建议的,但列表更具可读性,因为你不必将每个项目分成一对虚线。
还有一个类似搜索的功能:
- (setq L
- '(
- (idx name phone address)
- (1 "Alan" 7676 "Washington DC")
- (2 "Brian" 2424 "Boston")
- (3 "Chad" 3232 "NY")
- (4 "Derek" 0321 "Florida")
- (5 "Amber" 3021 "Boston")
- (6 "Cortney" 1210 "Florida")
- (7 "Andrew" 9292 "Texas")
- )
- )
- ; _$ (FindItems 'name "C*" L) -> ((3 "Chad" 3232 "NY") (6 "Cortney" 1210 "Florida"))
- ; _$ (FindItems 'address "B*" L) -> ((2 "Brian" 2424 "Boston") (5 "Amber" 3021 "Boston"))
- ; _$ (FindItems 'phone "3*" L) -> ((3 "Chad" 3232 "NY") (4 "Derek" 321 "Florida") (5 "Amber" 3021 "Boston"))
- ; _$ (FindItems 'name "H*" L) -> nil
- (defun FindItems ( key val L / ToStr i aL nL )
- (setq ToStr (lambda (x) (strcase (cond ((eq 'STR (type x)) x) ((vl-prin1-to-string x))))))
- (setq val (ToStr val))
- (setq aL (apply 'mapcar (cons 'list L)))
- (setq i 0)
- (if
- (setq nL
- (apply 'append
- (mapcar
- (function
- (lambda (x)
- (setq i (1+ i))
- (if (wcmatch (ToStr x) val)
- (list i)
- )
- )
- )
- (cdr (assoc key aL))
- )
- )
- )
- (mapcar (function (lambda (x) (nth x L))) nL)
- )
- ); defun FindItems
电话键有点问题——如果所有原子都是字符串,可能会更好? |