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

Convert a list with string or lists of strings to text

5 REPLIES 5
Reply
Message 1 of 6
mracad
799 Views, 5 Replies

Convert a list with string or lists of strings to text

Someday the light bulb is going off and I will understand mapcar and lambda.

However.....

 

I have a program that reads an excel file sheets. Sometimes the sheet only has 1 column, sometimes 2 or more.

I need to convert the saved values from the sheet to a list with the text spaced evenly.

Unfortunately the text lengths vary.

I would like a subroutine to pass the list to and return a list of strings with text aligned with padded spaces allowing for the longest text in each column.

I am using the new list in a dialog box with fixed_width_font=true (so columns align).

I hope this is clearer than mud!

 

Example 1-

(setq List1 (list "Col1-Line1" "Col1-Line2" "Col1-Line3-longer" "Col1-Line4"))

(AlignText List1)

returns

"Col1-Line1"

"Col1-Line2"

"Col1-Line3-longer"

"Col1-Line4"

so no padding needed

 

Example 2 -

(setq List2 (list (list "Col1-Line1" "Col1-Line2" "Col1-Line3-longer" "Col1-Line4") (list "Col2-Line1-longer" "Col2-Line2" "Col1-Line3" "Col2-Line4")))

(AlignText List1)

returns

"Col1-Line1        Col2-Line1-longer"

"Col1-Line2        Col2-Line2       "

"Col1-Line3-longer Col2-Line3      "

"Col1-Line4        Col2-Line4      "

 

 Example 3 -

(setq List3 (list (list "Col1-Line1" "Col1-Line2-longer" "Col1-Line3" "Col1-Line4") (list "Col2-Line1-longer" "Col2-Line2" "Col1-Line3" "Col2-Line4") (list "Col3-Line1-longer" "Col3-Line2" "Col1-Line3" "Col3-Line4")))

 

returns

"Col1-Line1        Col2-Line1-longer Col3-Line1-longer"

"Col1-Line2-longer Col2-Line2       Col3-Line2       "

"Col1-Line3-longer Col2-Line3      Col1-Line3       "

"Col1-Line4        Col2-Line4      Col3-Line4       "

 

 

5 REPLIES 5
Message 2 of 6
hgasty1001
in reply to: mracad

Hi,

 

Try finding the max length of the strings in each list, this will be say N, then the needed spaces will be the difference between N and the current string length +1.

 

To find N you can use something like (setq N  (apply 'max (mapcar 'strlen strlist)))

 

Gaston Nunez

Message 3 of 6
alanjt_
in reply to: hgasty1001

Dirty, but I *think* it does what you want: 

 

(defun _listMatch (mainList / n finalList a item)
  (foreach lst mainList
    (setq n -1)
    (while (setq item (nth (setq n (1+ n)) lst))
      (if (setq a (assoc n finalList))
        (setq finalList (subst (cons n (cons "\t" (cons item (cdr a)))) a finalList))
        (setq finalList (cons (list n "\t" item) finalList))
      )
    )
  )

  (if finalList
    (reverse
      (mapcar (function (lambda (l) (apply 'strcat (vl-remove nil (reverse (cddr l)))))) finalList)
    )
  )
)

 

Message 4 of 6
stevor
in reply to: mracad

The LISP functions like mapcar were made to handle lists faster, especially when special CPU processors were made for LISP. Today, execution speed is usually insignificant. And you can use other functions, although when you own language includes these functions, you will write code in less time. You can arrange the text by the TAB characters, or just extra SPACE characters. The font type, in Autocad, or elsewhere, also can effect the appearance. 2 steps: parse each line to 'words, or cells; then space them to make columns. A common parsing code; ; parse refstr by deLimstr in to List of strs (defun pars_s (rs / L i a b Q n) (if (and rs (= 'STR (type rs)) (setq Q (strLen rs) i 1 d " ")) (whiLe (<= i Q) (whiLe (and (= (substr rs i 1) d) (<= i Q)) (setq i (1+ i)) ) (setq n i) (whiLe (and (/= (substr rs i 1) d) (<= i Q)) (setq i (1+ i)) ) (setq a (substr rs n (- i n)) i (1+ i) ) (if (and a (/= "" a)) (setq L (cons a L)))) ) (if L (reverse L)) )
S
Message 5 of 6
pbejse
in reply to: mracad


@mracad wrote:

...I hope this is clearer than mud!....

 

 Example 3 -

(setq List3 (list (list "Col1-Line1" "Col1-Line2-longer" "Col1-Line3" "Col1-Line4") (list "Col2-Line1-longer" "Col2-Line2" "Col1-Line3" "Col2-Line4") (list "Col3-Line1-longer" "Col3-Line2" "Col1-Line3" "Col3-Line4")))

 

returns:

"Col1-Line1        Col2-Line1-longer Col3-Line1-longer"

"Col1-Line2-longer Col2-Line2       Col3-Line2       "

"Col1-Line3-longer Col2-Line3      Col1-Line3       "

"Col1-Line4        Col2-Line4      Col3-Line4       " 

 


Well not really, [at least for me that is]

 

Its that how it appears from reading the cell? How's about showing us how you want it to to look?

 

Are you wanting the dtermine the maximum number of concantenated string length? if so:

 

(setq N  (apply 'max (mapcar 'strlen strlist)));<--gasty1001

 

 

Then used that as basis for padding? ore remove the extra " " from the cell value?

 

Message 6 of 6
stevor
in reply to: hgasty1001

Using the code parts to get a padded result :

 

 (defun Ls_LSQ ( L ) (apply 'max (mapcar 'strlen l))) ; /Gasty-Pbe

 

; Aft pad target string S with CHR C for total length of Q

(defun S_AftPad (C Q S )

   (while (< (strlen S) Q) (setq S (strcat S C))) S)

    

 ; for the padded list of lists

  (setq psc " "    

           llsq (apply 'max (mapcar 'Ls_LSQ sll ))

           psll (mapcar '(lambda (sl)

                   (mapcar '(lambda (s) (S_AftPad psc llsq s)) sl)

                     ) sll) )

 

S

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

Post to forums  

Autodesk Design & Make Report

”Boost