Code Red > AutoLISP (Vanilla / Visual)
Generate Matrix List
(1/1)
Grrr1337:
Hi guys,
Anyone bored ? Well heres some challenge that might be interesting:
How do you write a function that generates matrix list by a given string (that represents excel's cell) i.e.:
--- Code - Auto/Visual Lisp: ---_$ (GenerateMatrixList "C3")'(("A1" "A2" "A3") ("B1" "B2" "B3") ("C1" "C2" "C3"))
:thinking:
Lee Mac:
Quickly written:
--- Code - Auto/Visual Lisp: ---(defun excelmatrix ( s / foo bar a b ) (defun foo ( c ) (if (/= c a) (cons (bar c 1) (foo (LM:alpha++ c))) (list (bar c 1)) ) ) (defun bar ( c r ) (if (<= r b) (cons (strcat c (itoa r)) (bar c (1+ r)))) ) (setq a (vl-string-right-trim "0123465789" (strcase s)) b (atoi (substr s (1+ (strlen a)))) ) (foo "A")) ;; Alpha++ - Lee Mac;; Increments an uppercase alphabetical string by one, e.g. AZ => BA;; a - [str] uppercase alphabetical string (defun LM:alpha++ ( a ) ( (lambda ( f ) (vl-list->string (reverse (f (reverse (vl-string->list a)))))) (lambda ( l ) (if l (if (= 90 (car l)) (cons 65 (f (cdr l))) (cons (1+ (car l)) (cdr l)) ) '(65) ) ) ))
--- Code - Auto/Visual Lisp: ---_$ (excelmatrix "C3")(("A1" "A2" "A3") ("B1" "B2" "B3") ("C1" "C2" "C3"))
Lee Mac:
Another:
--- Code - Auto/Visual Lisp: ---(defun excelmatrix ( s / a b c l r ) (setq a (vl-string-right-trim "0123465789" (strcase s)) b (atoi (substr s (1+ (strlen a)))) c "A" ) (repeat b (setq l (cons (itoa b) l) b (1- b))) (while (/= a c) (setq r (cons (mapcar '(lambda ( x ) (strcat c x)) l) r) c (LM:alpha++ c) ) ) (reverse (cons (mapcar '(lambda ( x ) (strcat c x)) l) r))) ;; Alpha++ - Lee Mac;; Increments an uppercase alphabetical string by one, e.g. AZ => BA;; a - [str] uppercase alphabetical string (defun LM:alpha++ ( a ) ( (lambda ( f ) (vl-list->string (reverse (f (reverse (vl-string->list a)))))) (lambda ( l ) (if l (if (= 90 (car l)) (cons 65 (f (cdr l))) (cons (1+ (car l)) (cdr l)) ) '(65) ) ) ))
Grrr1337:
Hi Lee,
I don't understand a thing in your first code * scratches head * (cause of the passing variables between the subfunctions and aswell they use recursions), but the important is that it works.
Good job, you brought me the nostalgia - back when I was lisp newbie and you still were providing overwhelming solutions. :uglystupid2:
Atleast I understood the iterative version. :)
This function is handy for experimenting with tables (generating/manipulating).
Navigation
[0] Message Index
Go to full version