Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

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

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
tmhpoole
2838 Views, 5 Replies

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

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

5 REPLIES 5
Message 2 of 6
tmhpoole
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 ..   

Message 3 of 6
pbejse
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

 

 

Message 4 of 6
tmhpoole
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! 🙂

 

* 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

Message 5 of 6
tmhpoole
in reply to: pbejse

Thank you, again pbejse!

 

It worked perfectly!  🙂

 

 - Theresa

Message 6 of 6
pbejse
in reply to: tmhpoole


@tmhpoole wrote:

Thank you, again pbejse!

 

 - Theresa


You are welcome Theresa Smiley Happy

Glad i could help

Cheers

 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost