Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
@mracad wrote:
I would like to convert a list of string values, to a list of strcat text with ", " between values, but limit the length of each strcat text ....
Here's one way [limited testing]:
(defun SL2CSL ; = String List to Concatenated Strings with Length limit
(oldlist lim / str newlist)
(while oldlist ; still something left?
(setq
str (car oldlist)
oldlist (cdr oldlist)
); setq
(while
(and
(car oldlist)
(< (+ (strlen str) (strlen (car oldlist))) (- lim 2))
); and
(setq
str (strcat str ", " (car oldlist))
oldlist (cdr oldlist)
); setq
); while
(setq newlist (cons str newlist))
); while
(reverse newlist)
); defun
Usage [with your list-variable name and limit number]:
(SL2CSL Nums 50)
@Kent1Cooper wrote:
....(< (+ (strlen str) (strlen (car oldlist))) (- lim 2))
....
Whoops -- that line should be either:
(<= (+ (strlen str) (strlen (car oldlist))) (- lim 2))
or [a few characters less code]:
(< (+ (strlen str) (strlen (car oldlist))) (1- lim))
That is, to use your example limit of 50, if the accumulated string so far plus the next piece come to a total of less than or equal to 48 characters, or to less than 49, then there's "room" to add that next piece plus the comma and space to separate it, without the total exceeding 50.