明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1311|回复: 2

[讨论] 表中元素的替换

[复制链接]
发表于 2021-9-26 06:57:36 | 显示全部楼层 |阅读模式
;;表中元素的替换---(支持内嵌5层表)的成员,但不支持点对表
;;(replst "a" "b" '(("1a" 5 ((("c" "a"))) "wrf" "c")("a" ("a" "c"))));返回(("1a" 5 ((("c" "a"))) "wrf" "c") ("b" ("b" "c")))
;;在论坛中没有找到关于表中元素的替换,查找成员也只有member函数,但不支持表中表的元素。所以自己写了一个,但内嵌表层数方法不好,请大家帮忙修改一下。
(defun replst ( old new l / )
        (cond        ((equal l nil)
                                nil)
                        ((equal old (car l))
                                (cons new (replst old new (cdr l))))
                        ((and (listp (car l))
                                        (member old (car l)))
                                (cons (replst old new (car l)) (replst old new (cdr l))))
                        ((and (listp (car l))
                                        (listp (car (car l)))
                                        (member old (car (car l))))
                                (cons (cons (replst old new (car (car l)))
                                                                        (replst old new (cdr (car l)))
                                                                )
                                                        (replst old new (cdr l))
                                ))
                        ((and (listp (car l))
                                        (listp (car (car l)))
                                        (listp (car (car (car l))))
                                        (member old (car (car (car l)))))
                                (cons (cons (list (replst old new (car (car (car l)))))
                                                                                (replst old new (cdr (car l)))
                                                                )
                                                        (replst old new (cdr l))
                                ))
                        ((and (listp (car l))
                                        (listp (car (car l)))
                                        (listp (car (car (car l))))
                                        (listp (car (car (car (car l)))))
                                        (member old (car (car (car (car l))))))
                                (cons (cons (list (list (replst old new (car (car (car (car l)))))))
                                                                                (replst old new (cdr (car l)))
                                                                )
                                                        (replst old new (cdr l))
                                ))
                        ((and (listp (car l))
                                        (listp (car (car l)))
                                        (listp (car (car (car l))))
                                        (listp (car (car (car (car l)))))
                                        (listp (car (car (car (car l)))))
                                        (member old (car (car (car (car (car l)))))))
                                (cons (cons (list (list (list (replst old new (car (car (car (car (car l)))))))))
                                                                                (replst old new (cdr (car l)))
                                                                )
                                                        (replst old new (cdr l))
                                ))
                        (T
                                (cons (car l) (replst old new (cdr l))))
        )
)
"觉得好,就打赏"
还没有人打赏,支持一下
发表于 2021-9-26 09:09:06 | 显示全部楼层
;函数:SubstItem
;功能:替换表中所有旧项(支持任意嵌套表)
;函数代码:
;;;By xq4u 2011-7-24
(defun SubstItem (newitem olditem lst / x tmplst)
  (foreach x (Subst newitem olditem lst)
    (if (listp x)
      (setq tmplst (append tmplst (list (SubstItem newitem olditem x))))
      (setq tmplst (append tmplst (list x)))
    )
  )
  tmplst
)
;语法:
;(SubstItem newitem olditem lst)
;参数:
;newitem:要替换到的内容
;olditem:被查找的内容
;lst:要替换的表
;返回值:
;返回修改后所得的表
;说明:在表中搜索某旧项,并将表中出现的每一个旧项用新项代替,然后返回修改后所得的表(支持任意嵌套表)

评分

参与人数 2明经币 +2 收起 理由
USER2128 + 1 很给力!
wrf610051 + 1 赞一个!

查看全部评分

 楼主| 发表于 2021-9-26 15:54:26 | 显示全部楼层
wzg356 发表于 2021-9-26 09:09
;函数:SubstItem
;功能:替换表中所有旧项(支持任意嵌套表)
;函数代码:

非常感谢,原来是我没有搜到。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2025-5-16 11:26 , Processed in 0.153997 second(s), 27 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表