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

Convert list of text values to list of strcat text with maximum strlen

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
mracad
670 Views, 2 Replies

Convert list of text values to list of strcat text with maximum strlen

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 Example - (setq Nums (list "1-1-100" "1-1-101" "1-1-102" "1-1-103" "1-1-104" "1-1-105" "1-1-106" "1-1-107" "1-1-108" "1-1-109")) (Nums2list Nums 50) 50 is maximum string length would return the following list with 2 text values - ("1-1-100, 1-1-101, 1-1-102, 1-1-103, 1-1-104, 1-1-105" "1-1-106, 1-1-107, 1-1-108, 1-1-109") Example - (setq Nums (list "1-1-100" "1-1-101" "1-1-102" "1-1-103" "1-1-104")) (Nums2list Nums 50) would return the following list with 1 text value- ("1-1-100, 1-1-101, 1-1-102, 1-1-103, 1-1-104")
2 REPLIES 2
Message 2 of 3
Kent1Cooper
in reply to: mracad


@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)

Kent Cooper, AIA
Message 3 of 3
Kent1Cooper
in reply to: Kent1Cooper


@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.

Kent Cooper, AIA

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

Post to forums  

Autodesk Design & Make Report

”Boost