Hi-
I am working on a routine (FULLSORT) that will:
(defun FULLSORT () ;FIRST DEFINE THE FUNCTION THAT WILL TURN THE TXT FILE INTO A LIST: (defun filetolist (f / *list* ltext f*1) (setq *list* '()) (setq f*1 (open f "r")) (while (setq ltext (read-line f*1)) (setq *list* (cons ltext *list*)) ) (close f*1) (setq *list* (reverse *list*)) ) ;THEN DEFINE THE VARIABLE THAT WILL HOLD THE CONTENTS OF THAT LIST AND SORT IT ALPHA-NUMERICALLY (ALTHOUGH THE NUMBERS AT THE END OF THE "LMH-..." AREN'T BEING SORTED PROPERLY: (SETQ RE-SORTED-LIST (ACAD_STRLSORT (filetolist "C:\\PRE-SORTED-FILE.TXT")) ) ;DEFINE THE NEW FILE TO HOLD THE PROPERLY SORTED LIST (setq filenameandlocation "C:\\POST-SORTED-FILE.TXT") (setq datafile (open filenameandlocation "w")) ;WRITE THE PROPERLY SORTED VALUES TO THE SECOND FILE (write-line RE-SORTED-LIST; THIS IS WHERE I NEED TO HAVE THIS VARIABLE BE A STRING datafile ) (close datafile) )
The problem I have is that instead of being sorted like in "POST-SORTED-FILE.TXT", my LIST looks like the one at "WRONG-SORTED-FILE.TXT". And because it isn't a STRING, it won't write out to the file I want it to.
I'm probably creating too many steps for myself - please help!
-T
Solved! Go to Solution.
Solved by pbejse. Go to Solution.
I have been able to hunt deeper into more threads, here and I think I found a method to handle the part about writing the LIST out to the file here:
Now to just get the LIST to sort properly, alpha-numerically ..
Your post-sorted-list is missing this one
"LMH-1-1-B-7,CONF RM C-01.5" <<--- ?
........
(SETQ RE-SORTED-LIST
(pbe:sorted (filetolist "C:\\PRE-SORTED-FILE.TXT"))
)
...
(foreach itm RE-SORTED-LIST
(write-line itm datafile)
)
(close datafile)
....
(defun pbe:sorted (lst_ / conv ) ;;;http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Sorting-a-List/m-p/3614040#M306384 (setq conv (lambda (k mode) (setq x (vl-string-position 44 k)) (if (zerop mode) (abs (atoi (substr k (1- x)))) (substr (setq k (substr k 1 x)) 1 (vl-string-position 45 k nil t)) ))) (mapcar 'last (vl-sort (mapcar '(lambda (j) (list (conv j 1)(conv j 0) j)) lst_) '(lambda (a b) (cond ((< (car a) (car b)) t) ((= (car a) (car b)) (< (cadr a) (cadr b))) ) ) ) ) )
HTH
Hi pbejse!
Thank you so much pointing me in the right direction- I'm heading into the office early so I can test out your code! 🙂
* thanks for pointing out the missing line, I was creating the .txt files from a combo of AutoCAD & using Excel, by hand ... I may have caught the duplicate value at the LMH-1-1-B-7 in my AutoCAD test file and removed later on.
-T