sadhu 发表于 2022-7-6 08:42:23

如何访问列表元素

我有BlktagValLst块列表,其中包含如下组织的列表元素:
 
(blknme-A tag1 tagvlu1A)
(blknme-A tag2 tagvlu2A)
(blknme-A tag3 tagvlu3A)
 
(blknme-B tag1 tagvlu1B)
(blknme-B tag1 tagvlu1B)
(blknme-B tag1 tagvlu1B)
 
等等,对于大量的块。
 
我需要访问标记值,所以我尝试了以下方法:

(and (= (car (assocblknme-A BlktagValLst)) blknme-A) ;if block name is blknme-A
      (= (cadr (assoc blknme-A BlktagValLst)) tag1) ; and tag is tag1
      (setq pntxt (caddr blknme-A BlktagValLst)))) ; ;save tag value in pntxt


(and (= (car (assocblknme-A BlktagValLst)) blknme-A) ;if block name is blknme-A
      (= (cadr (assoc blknme-A BlktagValLst)) tag2) ; and tag is tag2
      (setq descrixt (caddr blknme-A BlktagValLst)))) ; ;save tag value in descrixt


 
需要帮助才能实现这一功能,或者需要更好的方式。
 
谢谢

MSasu 发表于 2022-7-6 08:54:11

相关列表的内容是虚线对而不是标准列表(点条目除外);因此,应通过CDR而不是CADR访问第二个条目。此外,您不需要验证DXF键(汽车项目)。
 
当做
米尔恰

Lee Mac 发表于 2022-7-6 09:04:51

我建议将块数据重组为:
 
然后任务变成:
 
(cdr (assoc "TagName" (cdr (assoc "BlockName" lst))))

sadhu 发表于 2022-7-6 09:22:00

谢谢李,
我正在研究你的建议。

Lee Mac 发表于 2022-7-6 09:22:52

如果要使用原始数据,请使用以下三个函数从块/标记名中检索值:
 
(defun GetValue ( block tag lst / value )
   (while (and (setq item (car lst)) (not value))
       (if (and (eq block (car item)) (eq tag (cadr item)))
         (setq value (caddr item))
       )
       (setq lst (cdr lst))
   )
   value
)


(defun GetValue2 ( block tag lst )
   (vl-some '(lambda ( item ) (if (and (eq block (car item)) (eq tag (cadr item))) (caddr item))) lst)
)

(defun GetValue3 ( block tag lst / _massoc )

   (defun _massoc ( key lst / item )
       (if (setq item (assoc key lst))
         (cons (cdr item) (_massoc key (cdr (member item lst))))
       )
   )

   (cadr (assoc tag (_massoc block lst)))
)

 
例如。:
 
(setq lst
'(
       ("Block1" "Tag1" "b1Value1")
       ("Block1" "Tag2" "b1Value2")
       ("Block1" "Tag3" "b1Value3")
       ("Block2" "Tag1" "b2Value1")
       ("Block2" "Tag2" "b2Value2")
       ("Block2" "Tag3" "b2Value3")
   )
)
 
_$ (GetValue "Block2" "Tag2" lst)
"b2Value2"
_$ (GetValue2 "Block2" "Tag2" lst)
"b2Value2"
_$ (GetValue3 "Block2" "Tag2" lst)
"b2Value2"
 
快速测试:
(setq i 9)
(repeat 9
   (setq j 9)
   (repeat 9
       (setq l (cons (list (strcat "Block" (itoa i)) (strcat "Tag" (itoa j)) (strcat "b" (itoa i) "Value" (itoa j))) l))
       (setq j (1- j))
   )
   (setq i (1- i))
)

 
(
("Block1" "Tag1" "b1Value1")
("Block1" "Tag2" "b1Value2")
("Block1" "Tag3" "b1Value3")
("Block1" "Tag4" "b1Value4")
("Block1" "Tag5" "b1Value5")
("Block1" "Tag6" "b1Value6")
("Block1" "Tag7" "b1Value7")
("Block1" "Tag8" "b1Value8")
("Block1" "Tag9" "b1Value9")
("Block2" "Tag1" "b2Value1")
("Block2" "Tag2" "b2Value2")
...
("Block9" "Tag9" "b9Value9")
)
 
_$ (GetValue "Block6" "Tag7" l)
"b6Value7"
_$ (GetValue2 "Block6" "Tag7" l)
"b6Value7"
_$ (GetValue3 "Block6" "Tag7" l)
"b6Value7"
 
11

sadhu 发表于 2022-7-6 09:41:38

我选择了用这个——它是最快的,不是吗。
它是这样集成的
实际上,它是构成简单BOM的代码的一部分。(零件号、说明和数量)。
 
它工作得很好。非常感谢李。
 
我已经试过你的COUNT lisp-它不会打印属性,所以我的情况不是这样。
 
现在,我正在研究将这些数据放入一个可以插入到图形中的表中(如COUNT lisp)
 
再次感谢。

Lee Mac 发表于 2022-7-6 09:52:05

不客气,萨杜!祝你的计划顺利
页: [1]
查看完整版本: 如何访问列表元素