fixed-length text

fixed-length text

Anonymous
Not applicable
1,396 Views
11 Replies
Message 1 of 12

fixed-length text

Anonymous
Not applicable

Hello
What is the best way to deal with this situation?

I have an external text file.

"BK;1/1000;JRN10069;GG1;-3.223504;-40.696405" ;external file text

I would like to leave you this way 

"BK      1/1000       JRN10069     GG -3.223504    -40.696405    "  objective

The text 1 to the first (;) can only have a maximum length of 7 characters. If the text is more than 7, return only the first 7. If less than 7, complete with spaces.


The text 2 Second (;) can only have a maximum length of 12 characters. If the text is more than 12 return only the first 12. If it is less than 12 complete with spaces.

[...]


The text 4 fourth (;) can only have a maximum length of 2 characters. If the text has more than 2 return only the first 2. If it has less than 2 complete with spaces.

 

Okay, I can transform the string into a list and manipulate them independently, but how to deal with fixed length? not letting an item be different from what is allowed?

 

Accepted solutions (2)
1,397 Views
11 Replies
Replies (11)
Message 2 of 12

chriscowgill7373
Advisor
Advisor

 I think you would want this:

(substr string start [length] )

Returns a substring of a string

https://knowledge.autodesk.com/fr/search-result/caas/CloudHelp/cloudhelp/2015/FRA/AutoCAD-AutoLISP/f...


Christopher T. Cowgill, P.E.

AutoCAD Certified Professional
Civil 3D Certified Professional
Civil 3D 2024 on Windows 10

Please select the Accept as Solution button if my post solves your issue or answers your question.

Message 3 of 12

Anonymous
Not applicable

I know these functions, but that's not what I need.

0 Likes
Message 4 of 12

chriscowgill7373
Advisor
Advisor

I guess I dont understand what you are wanting.  You separate the items so you can look at them individually, then feed their string through the function, and spit back out the desired output into cad that meets the requirements.  If you are looking to do something with an existing external file that you want to create a new one, you can write back out to the text file.


Christopher T. Cowgill, P.E.

AutoCAD Certified Professional
Civil 3D Certified Professional
Civil 3D 2024 on Windows 10

Please select the Accept as Solution button if my post solves your issue or answers your question.

0 Likes
Message 5 of 12

pbejse
Mentor
Mentor

@Anonymous wrote:
"BK;1/1000;JRN10069;GG1;-3.223504;-40.696405" ;external file text

I would like to leave you this way 

"BK      1/1000       JRN10069     GG -3.223504    -40.696405    "  objective

The text 1 to the first (;) can only have a maximum length of 7 characters. If the text is more than 7, return


Post another another sample @Anonymous , one that is over the maximum number of characters and show us the results.

 

Also, post your string to list function here, we will incorporate that on the final code. 

 

 

0 Likes
Message 6 of 12

Anonymous
Not applicable

I'm trying to create a function that writes to a list_box
it is like this:

 

t1.PNG

objective:

t2.PNG

 

 

example text file

DT;17/1000;JRN10069;TR;-3.223504;-40.696405
DT;21/2000;JRN10069;TR;-3.222978;-40.696062
CC;21/2000;JRN10069;TR;-3.223516;-40.695864

 

 

here is the function:

 

(defun :Redlist_box ( )
  (setq name (getfiled "Select *.csv"  (getvar 'dwgprefix) "txt;csv" 16))
  (setq dados (open name "r"))
  (setq lst '(""))
  (while (setq line (read-line dados))
    (setq lst (append lst (list line)))
  )
)

(:Redlist_box)

 

;$!lst  (real)
;$ ("" "DT;17/1000;JRN10069;TR;-3.223504;-40.696405" "DT;21/2000;JRN10069;TR;-3.222978;-40.696062" "CC;21/2000;JRN10069;TR;-3.223516;-40.695864")

;objective
;$ ("" "DT               17/1000        JRN10069        TR                   -3.223504            -40.696405" "DT               21/2000        JRN10069        TR                   -3.222978            -40.696062" "CC               21/2000        JRN10069        TR                   -3.223516            -40.695864")
 
 
Each item has a maximum length and cannot be exceeded, if smaller, complete with spaces

 

0 Likes
Message 7 of 12

pbejse
Mentor
Mentor
Accepted solution

@Anonymous wrote:

I'm trying to create a function that writes to a list_box
....

example text file

DT;17/1000;JRN10069;TR;-3.223504;-40.696405
DT;21/2000;JRN10069;TR;-3.222978;-40.696062
CC;21/2000;JRN10069;TR;-3.223516;-40.695864
 

What were going to do is skip the string to list bit and process the string as it is.

You should be able to put this together @Anonymous 

 

 

 

(defun _padSpace ( s l)
	(if (< (strlen s) l) (_padSpace (strcat s " ") l) s)
 )
			
(defun _Breakdown (Str)    
(strcat (apply 'strcat    
  (mapcar '(lambda (n / p part)
	     (setq p (vl-string-position 59 str))
	     (setq part
	     	(cond
		       	(( < p n) (_padSpace (substr str 1 p) n))
			(( >= p n) (strcat (substr str 1 (1- n)) " "))
		)
	       )
	     (setq str (substr str (+ 2 p)))
	     part)   
	'(8 13 13 3 13))
      )
    str  
    )	
 )

 

Sample run

 

(foreach itm
	 '("DT;17/1000;JRN10069;TR;-3.223504;-40.696405"
	   "DT;21/2000;JRN10069;TR;-3.222978;-40.696062"
	   "CC;21/2000;JRN10069;TR;-3.223516;-40.695864"
	   "BK;1/1000;JRN10069;GG1;-3.223504;-40.696405"
	   "PBEJSEFORCETHIS;Frjuniornogueira;JRN10069;Autodesk;-3.223504;-40.696405"
	  )

  (print (_breakdown itm))(princ)
)

"DT      17/1000      JRN10069     TR -3.223504    -40.696405" 
"DT      21/2000      JRN10069     TR -3.222978    -40.696062" 
"CC      21/2000      JRN10069     TR -3.223516    -40.695864" 
"BK      1/1000       JRN10069     GG -3.223504    -40.696405" 
"PBEJSEF Frjuniornogu JRN10069     Au -3.223504    -40.696405" 

 

HTH

EDIT: Not sure about the last "part" < -40.696405 > . i will leave you to deal with that.

This includes the last part with spaces "   " , play around with the value until it suites you.

 

(defun _Breakdown (Str)    
(apply 'strcat    
  (mapcar '(lambda (n / p part)
	     (setq p (vl-string-position 59 str))
	     (setq part
	     	(cond
		       	(( < p n) (_padSpace (substr str 1 p) n))
			(( >= p n) (strcat (substr str 1 (1- n)) " "))
		)
	       )
	     
	     (setq str (if p (substr str (+ 2 p)) str ))
	     part)   
	'(8 13 13 3 13 13))
      )    
    )

 

 

0 Likes
Message 8 of 12

Anonymous
Not applicable

Wow!! You are impressive!! @pbejse 
Thank you!!

0 Likes
Message 9 of 12

hak_vz
Advisor
Advisor
Accepted solution

You will get it

 

 

(defun string_to_list ( str del / pos )
        (if (setq pos (vl-string-search del str))
            (cons (substr str 1 pos) (string_to_list (substr str (+ pos 1 (strlen del))) del))
            (list str)
        )
)
(defun trim_to_length (str len) (strcat (substr str 1 len) (substr "                   " 1 (- len (strlen str)))))
(setq category_lengths '(7 12 15 18))
(setq str (string_to_list  str ";"))
(mapcar '(lambda (x y) (trim_to_length x y)) str category_lengths)

 

My post is a few minutes late but maybe you can find it useful. 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 10 of 12

pbejse
Mentor
Mentor

@Anonymous wrote:

Wow!! You are impressive!! @pbejse 
Thank you!!


You are welcome Frjuniornogueira I'm sure you'll eventually come up with the solution on your own.

Glad it helps.

 

Cheers

 

Message 11 of 12

Anonymous
Not applicable

@hak_vz This also works very well, thanks !!

Message 12 of 12

pbejse
Mentor
Mentor

@hak_vz wrote:
(defun trim_to_length (str len) (strcat (substr str 1 len) (substr "                   " 1 (- len (strlen str)))))

I like the approach, nice idea  👍