TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: andrew_nao on July 20, 2009, 03:57:23 PM

Title: how can i add to the end of a certain item in a list
Post by: andrew_nao on July 20, 2009, 03:57:23 PM
I have a lisp that writes a list of info to a text file.
in this list i have a specific item that has less then 12 characters
(its always 8)
i would like take this specific list item and add spaced to the end of it to make it 12 characters long (its a format thing)

I tried adding the spaced in the beginning of the list item
but doing that ended up not displaying the list item.
sorta like it pushed it off the screen in the text file

here is what i used to add spaced to the beginning but returned undesired results.

Code: [Select]
(if (<(strlen (nth 0 dwg)) 12)
(setq dwg (subst "    " (nth 0 dwg) dwg))
)

does anyone have any code snippets to share that show me how to add spaced to the end of a specific list item

thanks for any help

never mind i figured out what i was doing wrong
thanks for looking

mods can remove this post
Title: Re: how can i add to the end of a certain item in a list
Post by: hermanm on July 20, 2009, 05:29:05 PM
Code: [Select]
;;;----------------------pad-or-trunc------------------------
;;;  Purpose: Functions to pad or truncate a string
;;;           to a designated width
;;;  by Herman Mayfarth
;;;  str  : string to process
;;;  char : pad character
;;;  len  : required length of output string
;;;--------------------------------------------------------
;;pad right or truncate right
(defun padright-or-trunc (str char len )
  (if (> (strlen str) len )
    (substr str 1 len)
    (repeat (- len (strlen str))
      (setq str (strcat str char))
    )
  );if
)

;;pad left or truncate left
(defun padleft-or-trunc (str char len / sl)
  (if (> (setq sl (strlen str)) len )
    (substr str (- sl len) len)
    (repeat (- len (strlen str))
      (setq str (strcat char str))
    )
  );if
)
Title: Re: how can i add to the end of a certain item in a list
Post by: DEVITG on July 21, 2009, 07:44:31 AM
Please try it

Code: [Select]
(setq dwg-list  (list"1234567890" "abcdefg" "34567"  "123456789012"    )   )

(defun add-to-n ( list$ char$ n /
dwg-list-1
DWG-LIST  ESP# ESP$ NEWDG
)
(foreach dwg list$
(setq esp$ "") 
(if (> (setq esp# (- n (strlen dwg)))0)
(Progn
  (repeat esp#
    (setq esp$ ( strcat esp$ char$))
    )
(setq newdg (strcat dwg esp$   ))
  (Setq dwg-list-1  (cons newdg dwg-list-1 ))

 );_ progn
(Setq dwg-list-1  (cons dwg dwg-list-1 ))
 
  );_ if
  );_ foreach
(setq dgw-list-2 (reverse dwg-list-1))
  )

(add-to-n dwg-list "&" 12)

Replace "&" by " "
Title: Re: how can i add to the end of a certain item in a list
Post by: CAB on July 21, 2009, 10:08:13 AM
Another:
http://www.theswamp.org/index.php?topic=27683.msg332386#msg332386