本帖最后由 cabinsummer 于 2012-5-7 22:31 编辑
数据文本test.txt结构- Index para1 para2 para3
- A 1 2 3
- B 10 20 30
- C 100 200 300
- D 1000 2000 3000
复制代码
用以下程序读取参数
- (defun get_para (filename id / fn para val)
- (setq fn (open (findfile filename) "r")) ;;;打开数据文件
- (setq para (read (strcat "(" (read-line fn) ")"))) ;;;读第一行
- (while ;;;读到索引为id的行
- (and (setq val (read-line fn))(not (eq id (car (setq val (read (strcat "(" val ")")))))))
- (setq val nil)
- )
- (close fn) ;;;关闭数据文件
- (if val (foreach x (setq mtr (mapcar 'cons para val))(set (car x) (cdr x))));;;参数赋值
- )
可以得到index为某个值时,各值存在变量para1 para2 para3中。
由于以表为参数,所以可以扩展任意数量的参数,也不在乎参数顺序。
程序运行完毕后,清理无用的自定义变量
- (foreach x mtr (set (car x) nil))
- (setq mtr nil)
示例:
- (get_para "test.txt" 'A) ;;;得到INDEX=A; PARA1=1; PARA2=2; PARA3=3
- (foreach x mtr (set (car x) nil)) ;;;清空
- (get_para "test.txt" 'B) ;;;得到INDEX=B; PARA1=10; PARA2=20; PARA3=30
- (foreach x mtr (set (car x) nil)) ;;;清空
- (get_para "test.txt" 'C) ;;;得到INDEX=C; PARA1=100; PARA2=200; PARA3=300
- (foreach x mtr (set (car x) nil)) ;;;清空
- (get_para "test.txt" 'D) ;;;得到INDEX=D; PARA1=1000; PARA2=2000; PARA3=3000
- (foreach x mtr (set (car x) nil)) ;;;清空
- (get_para "test.txt" 'E) ;;;得到INDEX=nil; PARA1=nil; PARA2=nil; PARA3=nil
|