Visual LISP, AutoLISP and General Customization

Visual LISP, AutoLISP and General Customization

Reply
Active Contributor
tmhpoole
Posts: 43
Registered: ‎05-06-2004
Message 1 of 6 (825 Views)
Accepted Solution

FILE to LIST, LIST to STRING, Sort Alpha-numeric, write to FILE

825 Views, 5 Replies
10-17-2012 03:39 PM

Hi-

 

I am working on a routine (FULLSORT)  that will:

 

 

  1. take an external TXT file (PRE-SORTED-FILE.TXT)
  2. turn its contents into a LIST
  3. re-sort that list ALPHA-NUMERICALLY
  4. turn that sorted LIST into a STRING
  5. and then write that string to an external TXT file (POST-SORTED-FILE.txt)
(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

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

 

 

Active Contributor
tmhpoole
Posts: 43
Registered: ‎05-06-2004
Message 2 of 6 (815 Views)

Re: FILE to LIST, LIST to STRING, Sort Alpha-numeric, write to FILE

10-17-2012 06:37 PM in reply to: tmhpoole

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:

 

http://forums.autodesk.com/t5/Visual-LISP-AutoLISP-and-General/Write-list-to-file-LISP/m-p/1727085/h...

 

Now to just get the LIST to sort properly, alpha-numerically ..   

*Expert Elite*
pbejse
Posts: 2,500
Registered: ‎11-24-2009
Message 3 of 6 (804 Views)

Re: FILE to LIST, LIST to STRING, Sort Alpha-numeric, write to FILE

10-17-2012 07:52 PM in reply to: tmhpoole

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

 

 

Active Contributor
tmhpoole
Posts: 43
Registered: ‎05-06-2004
Message 4 of 6 (785 Views)

Re: FILE to LIST, LIST to STRING, Sort Alpha-numeric, write to FILE

10-18-2012 02:18 AM in reply to: pbejse

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! :smileyhappy:

 

* 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

Active Contributor
tmhpoole
Posts: 43
Registered: ‎05-06-2004
Message 5 of 6 (760 Views)

Re: FILE to LIST, LIST to STRING, Sort Alpha-numeric, write to FILE

10-18-2012 01:13 PM in reply to: pbejse

Thank you, again pbejse!

 

It worked perfectly!  :-)

 

 - Theresa

*Expert Elite*
pbejse
Posts: 2,500
Registered: ‎11-24-2009
Message 6 of 6 (744 Views)

Re: FILE to LIST, LIST to STRING, Sort Alpha-numeric, write to FILE

10-18-2012 09:53 PM in reply to: tmhpoole

tmhpoole wrote:

Thank you, again pbejse!

 

 - Theresa


You are welcome Theresa :smileyhappy:

Glad i could help

Cheers

 

Need installation help?

Start with some of our most frequented solutions or visit the Installation and Licensing Forum to get help installing your software.