明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 3300|回复: 12

[函数] 分享自己编的编号字符串排序函数

[复制链接]
发表于 2015-1-27 09:42 | 显示全部楼层 |阅读模式
本帖最后由 ld_117 于 2015-1-27 10:51 编辑


分享自己编的编号字符串排序函数,本人只求实现功能,不求深入学习lisp,所以函数写的比较冗长,仅作参考....
(orderLst (lst keyW))
功能:对lst根据关键字进行排序并列出相同元素的个数
参数:lst 字符串数列  keyW 关键字字符串
例  :(orderLst '("KL5" "KL2(2)" "KL1a(2)" "KL2(2)" "KL1(1)") "KL")
        返回(("KL1(1)" . 1) ("KL1a(2)" . 1) ("KL2(2)" . 2) ("KL5" . 1))
作者:Ld_117

  1. (vl-load-com)        ;将 Visual LISP 扩展功能加载到 AutoLISP
  2. ;---------正则表达式设置---------
  3. (setq reg (vlax-create-object "vbscript.regexp"))   ;创建正则表达式
  4. (vlax-put-property reg 'global -1)                  ;是否匹配全部 (-1是 ,0 不是)
  5. (vlax-put-property reg 'Multiline -1)               ;是否多行匹配 (-1是 ,0 不是)
  6. (vlax-put-property reg 'IgnoreCase -1)              ;是否忽略大小写 (-1是 ,0 不是)
  7. ;---------正则表达式设置结束---------
  8. ;;排序函数开始
  9. ;;(orderLst (lst keyW))
  10. ;;功能:对lst根据关键字进行排序并列出相同元素的个数
  11. ;;参数:lst 字符串数列  keyW 关键字字符串
  12. ;;例  :(orderLst '("KL5" "KL2(2)" "KL1a(2)" "KL2(2)" "KL1(1)") "KL")
  13. ;;      返回(("KL1(1)" . 1) ("KL1a(2)" . 1) ("KL2(2)" . 2) ("KL5" . 1))
  14. ;;作者:Ld_117
  15. (defun orderLst (lst keyW / x nLst len i lLst tnLst nowLst nnLst)
  16.   (setq nLst '() tnLst '())
  17.   (foreach x lst
  18.     (setq nLst (cons (cons x (getNum x keyW)) nLst));将lst做成点对形式
  19.   )
  20.   ;获得排序后的列表
  21.   (foreach x (sortLst (reverse nLst))
  22.     (setq tnLst (append tnLst (list (car x))))
  23.   )
  24.   (setq
  25.     nLst tnLst
  26.     len (vl-list-length nLst)
  27.     i 0
  28.     nowLst '()
  29.     nNowLst '()
  30.     nnLst '()
  31.   )
  32.   ;合并tnLst中相同元素并统计数量
  33.   (while (< i len)
  34.     (if (setq nowLst (assoc (nth i nLst) nnLst))
  35.       (progn
  36.         (setq nNowLst (cons (car nowLst) (1+ (cdr nowLst))))
  37.         (setq nnLst (subst nNowLst nowLst nnLst))
  38.       )
  39.       (setq nnLst (cons (cons (nth i nLst) 1) nnLst))
  40.     )
  41.     (setq i (1+ i))
  42.   )
  43.   (setq nnLst (reverse nnLst))
  44. )
  45. ;;(getNum (str keyW))
  46. ;;功能:获得str中紧跟keyW的编号字符,编号可以包含数字及字母
  47. ;;参数:str 字符串  keyW 关键字字符串
  48. ;;例  :(getNum "KL5a(2)" "KL")
  49. ;;      返回"5a"
  50. ;;作者:Ld_117
  51. (defun getNum (str keyW / matchcollect)
  52.   (vlax-put-property reg 'pattern (strcat "^" keyW))
  53.   (setq str (vlax-invoke-method reg 'Replace str ""))
  54.   (vlax-put-property reg 'pattern "^[0-9a-zA-Z]+")
  55.   (setq matchcollect (vlax-invoke-method reg 'Execute str))
  56.   (vlax-for match_item matchcollect (setq str (car (list (eval (vlax-get-property match_item 'value))))))
  57.   str
  58. )
  59. ;;(sortLst (lst))
  60. ;;功能:调用vl-sort函数
  61. (defun sortLst (lst)
  62.   (vl-sort lst
  63.     'sortLstFun
  64.   )
  65. )
  66. ;;(sortLstFun (a b))
  67. ;;功能:函数vl-sort中的比较函数,返回值为t则vl-sort对表中a、b元素位置进行调换
  68. ;;参数:a 为lst中后一个列表元素  b 前一个列表元素
  69. ;;例  :(sortLstFun '("KL2" . "2") '("KL4" . "4"))
  70. ;;      返回t
  71. ;;作者:Ld_117
  72. (defun sortLstFun (a b / a1 a2 b1 b2 len1 len2 len loop t1 t2 i)
  73.   (setq
  74.     a1 (cdr a)
  75.     b1 (cdr b)
  76.     result t
  77.   )
  78.   (vlax-put-property reg 'pattern "^[0-9]+")
  79.   (setq matchcollect (vlax-invoke-method reg 'Execute (cdr a)))
  80.   (vlax-for match_item matchcollect (setq a1 (car (list (eval (vlax-get-property match_item 'value))))))
  81.   (setq a2 (vlax-invoke-method reg 'Replace (cdr a) ""))
  82.   (setq matchcollect (vlax-invoke-method reg 'Execute (cdr b)))
  83.   (vlax-for match_item matchcollect (setq b1 (car (list (eval (vlax-get-property match_item 'value))))))
  84.   (setq b2 (vlax-invoke-method reg 'Replace (cdr b) ""))
  85.   (if (> (atof a1) (atof b1))
  86.     (setq result nil)
  87.     (if (< (atof a1) (atof b1))
  88.       (setq result t)
  89.       (progn
  90.         (if (= a2 nil) (setq a2 ""))
  91.         (if (= b2 nil) (setq b2 ""))
  92.         (setq
  93.           len1 (strlen a2)
  94.           len2 (strlen b2)
  95.           len (max len1 len2)
  96.           loop t
  97.           i 1
  98.         )
  99.         (if (= len1 0)
  100.           (setq result t)
  101.           (if (= len2 0)
  102.             (setq result nil)
  103.             (while loop
  104.               (setq
  105.                 t1 (ASCII (substr a2 i 1))
  106.                 t2 (ASCII (substr b2 i 1))
  107.                 i (1+ i)
  108.               )
  109.               (if (< t1 t2)
  110.                 (setq loop nil result t)
  111.                 (if (> t1 t2)
  112.                   (setq loop nil result nil)
  113.                   (setq loop nil result nil)
  114.                 )
  115.               )
  116.             )
  117.           )
  118.         )
  119.       )
  120.     )
  121.   )
  122.   result
  123. )
  124. ;;排序函数结束




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

x

评分

参与人数 3明经币 +3 收起 理由
Bao_lai + 1 赞一个!
USER2128 + 1 神马都是浮云
lucas_3333 + 1 神马都是浮云

查看全部评分

本帖被以下淘专辑推荐:

发表于 2018-10-4 18:16 | 显示全部楼层
支持下楼主,下载学习了!!!!
发表于 2015-1-27 11:39 | 显示全部楼层
我第一个出来支持你!放出源码才是正道!
发表于 2015-1-27 14:31 | 显示全部楼层
我也支持你!放出源码才是正道!
 楼主| 发表于 2015-1-28 17:27 | 显示全部楼层
USER2128 发表于 2015-1-27 11:39
我第一个出来支持你!放出源码才是正道!

你们能看到源码吗,我不常发帖不知道操作的对不对
发表于 2015-1-28 21:20 | 显示全部楼层
ld_117 发表于 2015-1-28 17:27
你们能看到源码吗,我不常发帖不知道操作的对不对

发贴没有问题,他是在赞扬你放出源码
 楼主| 发表于 2015-1-28 23:53 | 显示全部楼层
lucas_3333 发表于 2015-1-28 21:20
发贴没有问题,他是在赞扬你放出源码

本来想快点升级设置回复可见的。。。捣弄半天没弄成...
发表于 2015-1-29 08:17 | 显示全部楼层
ld_117 发表于 2015-1-28 23:53
本来想快点升级设置回复可见的。。。捣弄半天没弄成...

论坛不鼓励回复可见的,所以一般等级没这个权限, 另外回复的多少跟你升级没有关系
发表于 2015-1-29 08:21 | 显示全部楼层
支持源码,
发表于 2015-1-30 14:33 | 显示全部楼层
支持下楼主,没钱就不下了
 楼主| 发表于 2015-1-30 17:07 | 显示全部楼层
emk 发表于 2015-1-30 14:33
支持下楼主,没钱就不下了

不是贴源码了吗,复制是一样的,lsp文件是留给土豪下载的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-4-26 16:05 , Processed in 0.721174 second(s), 37 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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