TheSwamp
Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Adesu on December 14, 2006, 02:59:35 AM
-
Hi Alls,
I have a problem to check number in file name,my file name have specific number,i.e
(setq file_folder "D:/YBI/Program/AutoLisp/Lisp program/My Alls Program")
(setq lst (cddr (vl-directory-files file_folder)))
("0001-Array Circle.LSP" "0002-View Bar M10 to Down.LSP" "0003-Symbol of Yuasa.LSP" "0004-Border and Title.LSP" "0005-ASBAK.LSP" "0006-Registration Of Drawing.LSP"....etc.
(setq len (length lst)) ; 490
(setq file_last (car (reverse lst))) ; "0491-Edit Text Location File.LSP"
I mean total number of file is 491 pcs,but the fact only 490,it mean minus 1 file,but I don't know where or what number not yet write.
Sorry if confuse,I give solution like this
(setq lst1 '(1 2 3 4 5 6 7 8 9 10))
(setq lst2 '(1 2 3 4 5 6 8 9 10))
Now you can imagine,how to know number 7 it need for lst2,how to compare it.
-
Hi,
Something like this ?
(defun test (lst)
(while (and lst (= (1+ (car lst)) (cadr lst)))
(setq lst (cdr lst))
)
(if (cdr lst)
(1+ (car lst))
)
)
(test lst1) -> nil
(test lst2) -> 7
Edit :
Or a recursive way :
(defun test (lst)
(if (and lst (= (1+ (car lst)) (cadr lst)))
(test (cdr lst))
(if (cdr lst)
(1+ (car lst))
)
)
)
-
Hi gile,
I just tested,it's great !!,thanks very much.
-
If it is necessary to check up only:
(defun test (lst)
(vl-every (function (lambda (a b) (= (1+ a) b))) lst (cdr lst))
) ;_ defun
-
If you have many breaks in the list or breaks big...
(defun test (lst)
(vl-remove-if
(function
(lambda (x)
(= (1+ (car x)) (cdr x))
) ;_ lambda
) ;_ function
(mapcar
(function cons)
lst
(cdr lst)
) ;_ mapcar
) ;_ vl-remove-if
) ;_ defun
(test '(1 2 3 4 5 6 8 9 10 13))
; => ((6 . 8) (10 . 13))
-
recursive function, for many breaks in the list or breaks big:
(defun test (lst)
(if (cdr lst)
(if (= (1+ (car lst)) (cadr lst))
(test (cdr lst))
(cons (cons (car lst) (cadr lst)) (test (cdr lst)))
) ;_ if
) ;_ if
) ;_ defun
(test '(1 2 3 4 5 6 8 9 10 13))
; => ((6 . 8) (10 . 13))
-
It's good ,I like it.
Thanks.
If you have many breaks in the list or breaks big...
(defun test (lst)
(vl-remove-if
(function
(lambda (x)
(= (1+ (car x)) (cdr x))
) ;_ lambda
) ;_ function
(mapcar
(function cons)
lst
(cdr lst)
) ;_ mapcar
) ;_ vl-remove-if
) ;_ defun
(test '(1 2 3 4 5 6 8 9 10 13))
; => ((6 . 8) (10 . 13))
-
> Adesu
Recursion works more quickly... :)