DGRL Posted March 28, 2018 Share Posted March 28, 2018 Dear coders, on my learning path to become a good lsp coder i am trying to manipulate lists can someone help me out? Take this list ( ("Circle" "6" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "5" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle1" "4" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle1" "3" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle2" "2" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle2" "1" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ) See first entry of every list. How can i make 3 new lists out of this list? Equal did not work for me so far and foreach also not Quote Link to comment Share on other sites More sharing options...
Grrr Posted March 28, 2018 Share Posted March 28, 2018 Hi, We called this "group by keys". By we, I mean me. EDIT: Oh, or perhaps you wanted just to extract the unique items, then try Lee's Unique with fuzz function. Quote Link to comment Share on other sites More sharing options...
DGRL Posted March 28, 2018 Author Share Posted March 28, 2018 @Grrrrrrrrrrrrrrrrr in sense of respect and not in not worthy The group by keys is what i need Quote Link to comment Share on other sites More sharing options...
Grrr Posted March 28, 2018 Share Posted March 28, 2018 No worries, I personally prefer Lee's suggestions in that "group by keys" thread, due simplicity and shortness. Quote Link to comment Share on other sites More sharing options...
DGRL Posted March 28, 2018 Author Share Posted March 28, 2018 Same here :-) Quote Link to comment Share on other sites More sharing options...
DGRL Posted March 29, 2018 Author Share Posted March 29, 2018 @Grrrrrr Besides the "group by keys" is there also a function that will determine if there are doubles in the list Like what I have now and rename them? I.E. Circle is 2 times in the list and I want to be able to rename the second 1 only SO if I have lets say 6 times same name I want 5 of them to have "-1" behind the name and off course this will add up +1 per name See example below ( ("Circle" "6" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-1" "5" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-2" "4" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-3" "3" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-4" "2" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-5" "1" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ) Quote Link to comment Share on other sites More sharing options...
Grrr Posted March 29, 2018 Share Posted March 29, 2018 Post a example list before, and example list after. Quote Link to comment Share on other sites More sharing options...
DGRL Posted March 29, 2018 Author Share Posted March 29, 2018 BEFORE ( ("Circle" "6" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "5" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "4" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "3" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "2" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "1" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("different name" "3" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("other name" "2" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("name" "1" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ) AFTER ( ("Circle" "6" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-1" "5" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-2" "4" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-3" "3" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-4" "2" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-5" "1" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("different name" "3" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("other name" "2" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("name" "1" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ) Quote Link to comment Share on other sites More sharing options...
Grrr Posted March 29, 2018 Share Posted March 29, 2018 Perhaps: (setq a ( (lambda ( / b i ) (setq i 0) (mapcar '(lambda (x) (if (= (car x) "Circle") (if b (cons (strcat "Circle-" (itoa (setq i (1+ i)))) (cdr x)) (progn (setq b t) x) ) x ) ); lambda '( ("Circle" "6" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "5" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "4" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "3" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "2" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "1" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("different name" "3" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("other name" "2" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("name" "1" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ) ) ) ) ) Quote Link to comment Share on other sites More sharing options...
DGRL Posted March 29, 2018 Author Share Posted March 29, 2018 @grrrrr Thanks I think that i can modify this to my needs and learn from it if I cant figure it out I will let you know lol BEER for you @grrrr BTW Where can I find more info about the MAPCAR and LAMBDA? Seems interesting to use this Quote Link to comment Share on other sites More sharing options...
Grrr Posted March 29, 2018 Share Posted March 29, 2018 @grrrrr Thanks I think that i can modify this to my needs and learn from it if I cant figure it out I will let you know lol BEER for you @grrrr Cheers BTW Where can I find more info about the MAPCAR and LAMBDA? Seems interesting to use this I would suggest This tutorial by Lee Mac. The first sentence he wrote there is very accurate: In my experience, the mapcar and lambda functions are two of the least understood functions in the AutoLISP programming language, however, when understood and used correctly, they can replace superfluous code and are powerful functions for dealing with lists. And the tutorial is excellent, but you have to pay enough attention and practice alot with it. But once you get used to it, IMO basically you've learned 60% of LISP. BTW Heres some more generic function: ; Numbers all string keys in Assoc List (defun NumberStrKeys ( aL / keys ) (cond ( (not (and (vl-consp aL) (vl-every 'vl-consp aL))) (prompt "NumberStrKeys: Invalid list.") ) ( (mapcar (function (lambda (L / k tmp i ) (if (eq 'STR (type (setq k (car L)))) (if (setq tmp (assoc k keys)) (if (setq i (cdr tmp)) (progn (setq keys (subst (cons k (setq i (1+ i))) tmp keys)) (cons (strcat k "-" (itoa i)) (cdr L)) ); progn (progn (setq keys (cons (cons k 0) keys)) L) ); if (progn (setq keys (cons (cons k 0) keys)) L) ); if L ); if ); lambda ); function aL ); mapcar ) ); cond ); defun NumberStrKeys Example usage & output: (NumberStrKeys '( (1 "1" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "6" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "5" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "4" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) (2 "2" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("RECTAN" "2" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("SQUARE" "3" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("SQUARE" "2" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "3" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "2" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) (3 "3" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("SQUARE" "1" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "1" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("RECTAN" "1" (-3.06162e-017 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ) ) >> ( (1 "1" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle" "6" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-1" "5" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-2" "4" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) (2 "2" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("RECTAN" "2" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("SQUARE" "3" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("SQUARE-1" "2" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-3" "3" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-4" "2" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) (3 "3" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("SQUARE-2" "1" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("Circle-5" "1" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ("RECTAN-1" "1" (-3.06162e-17 -0.5 0.0) 1.0 10 (0.0 0.0 0.0)) ) Quote Link to comment Share on other sites More sharing options...
DGRL Posted March 29, 2018 Author Share Posted March 29, 2018 :CHEERS: Thanks a lot m8 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.