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