svorgodne Posted September 26, 2017 Posted September 26, 2017 I have a list of lists I want to retrieve "nth 0 list" from this list with mapcar, obviously as a list Something like this (mapcar '(nth 0) ListOfLists) Is it possible? Thanks in advance Quote
Tharwat Posted September 26, 2017 Posted September 26, 2017 Hi, Replace it with (mapcar 'car ..... function. Quote
svorgodne Posted September 26, 2017 Author Posted September 26, 2017 Thanks Tharwat, still... Is it possible to make it with nth function? I want to understand how does it work the mapcar function and it should also make the trick with nth, right? Quote
Grrr Posted September 26, 2017 Posted September 26, 2017 Use (mapcar '(lambda (sublist) (nth 0 sublist)) ListOfLists) If you decide to change the nth to a bigger index (where car/cadr/caddr/cadddr won't help). Else Tharwat's suggestion is the most effective. Quote
Tharwat Posted September 26, 2017 Posted September 26, 2017 Yes sure, but in that case you need to use lambda function along with mapcar as long as you are not using the nth function alone with mapcar. Like this: (mapcar '(lambda (lst) (nth 0 lst)) (list '(0 1 2) '(3 4 5))) Quote
svorgodne Posted September 26, 2017 Author Posted September 26, 2017 Wow, I love this forum. It makes life easier to understand. Thanks guys Quote
BIGAL Posted September 26, 2017 Posted September 26, 2017 If you want a nth of nth can be done also in example a list of lists ((0 1)( 2 3)(4 5)) could go deeper using Z variable for a 3rd level. (setq y 0) (repeat (setq x (length lst)) (setq ans (nth y (nth (setq x (- x 1)) lst)) (setq y (+ y 1)) ) Quote
Grrr Posted September 26, 2017 Posted September 26, 2017 If you decide to change the nth to a bigger index (where car/cadr/caddr/cadddr won't help). Although the above made me think that the repeat function could be implemented, to create a generic subfunction: (defun GetNthColumn ( n aL ) (repeat n (setq aL (mapcar 'cdr aL)) ) (mapcar 'car aL) ) _$ (GetNthColumn 2 '( ("A1" "B1" "C1" "D1") ("A2" "B2" "C2" "D2") ("A3" "B3" "C3" "D3") ("A4" "B4" "C4" "D4") ) ) ("C1" "C2" "C3" "C4") Lee had posted a function like that somewhere. Quote
svorgodne Posted September 26, 2017 Author Posted September 26, 2017 I have finally a list extracted from RGB color code from all 255 basic AutoCAD colors with your kind suggestions. Each layer has a different color and through an iteration I'd have to assign the color to the correspondent layer. As an example I filtered the RGB code and the result is "100". I made a list as follows called LST_LYR_TMP which looks like this ("c" "100") I want to integrate this values (as arguments?) to a command in order to change the current layer color to "100" (command "-layer" LST_LYR_TMP "" ) At the end the function should do this (command "-layer" "c" "100" "" ) Sometimes color codes will be real RGB and then the list will be as follows ("c" "t" "0,255,63") I cannot make it work. Any suggestion? Thanks in advance Quote
marko_ribar Posted September 26, 2017 Posted September 26, 2017 1. ("c" "100") - add "_.-layer" in front and "" at the end : (apply (function command) '("_.-layer" "_c" "100" "")) similary 2. ("c" "t" "0,255,63") (apply (function command) '("_.-layer" "_c" "_t" "0,255,63" "" "")) BTW. Not sure how many "" at the end, so check it... Quote
marko_ribar Posted September 26, 2017 Posted September 26, 2017 @Grrr... Although the above made me think that the repeat function could be implemented, to create a generic subfunction: (defun GetNthColumn ( n aL ) (repeat n (setq aL (mapcar 'cdr aL)) ) (mapcar 'car aL) ) _$ (GetNthColumn 2 '( ("A1" "B1" "C1" "D1") ("A2" "B2" "C2" "D2") ("A3" "B3" "C3" "D3") ("A4" "B4" "C4" "D4") ) ) ("C1" "C2" "C3" "C4") Lee had posted a function like that somewhere. (setq lst '(("A1" "B1" "C1" "D1") ("A2" "B2" "C2" "D2") ("A3" "B3" "C3" "D3") ("A4" "B4" "C4" "D4"))) (nth 2 (apply 'mapcar (cons 'list lst))) => ("C1" "C2" "C3" "C4") Quote
Grrr Posted September 26, 2017 Posted September 26, 2017 @Grrr... (setq lst '(("A1" "B1" "C1" "D1") ("A2" "B2" "C2" "D2") ("A3" "B3" "C3" "D3") ("A4" "B4" "C4" "D4"))) (nth 2 (apply 'mapcar (cons 'list lst))) => ("C1" "C2" "C3" "C4") Ah, thanks Marko! I've forgot about the (apply 'mapcar ...) technique! Quote
svorgodne Posted September 26, 2017 Author Posted September 26, 2017 Thanks Marko. This is precisely what I want to avoid. To do it manually. There is different information for each layer and I want to make it automatic. I have a list of more than 100 layers that we are not using in every project phase, therefore to load them through an Excel file with all specifications it's more than necessary. The not economical solution was to open a new drawing which has all layers and copy entities with this layers into the drawing I am working on, right? Well, that is a lot of time consuming. What I have achieved is to overwrite all layers from an Excel file. Still the problem is that I get the RGB codes and the basic color codes (1-255) should be called like that otherwise, color 7 for example would be 255,255,255. So, my goal is to pass the values from the lists I provided before, as arguments to the running layer command. the values from the lists should be read and evaluated and in consequence assigned to the command substructure. Do you have any other idea? Thank you very much in advance Quote
rlx Posted September 26, 2017 Posted September 26, 2017 @Grrr... (setq lst '(("A1" "B1" "C1" "D1") ("A2" "B2" "C2" "D2") ("A3" "B3" "C3" "D3") ("A4" "B4" "C4" "D4"))) (nth 2 (apply 'mapcar (cons 'list lst))) => ("C1" "C2" "C3" "C4") Oh wow , that's a good one! gr. Rlx Quote
Lee Mac Posted September 26, 2017 Posted September 26, 2017 Just be aware of the following subtlety to the transpose approach: _$ (setq lst '(("a1" "b1" "c1" "d1") ("a2" "b2") ("a3" "b3" "c3") ("a4" "b4" "c4" "d4"))) (("a1" "b1" "c1" "d1") ("a2" "b2") ("a3" "b3" "c3") ("a4" "b4" "c4" "d4")) _$ (apply 'mapcar (cons 'list lst)) (("a1" "a2" "a3" "a4") ("b1" "b2" "b3" "b4")) _$ (nth 2 (apply 'mapcar (cons 'list lst))) nil _$ (mapcar '(lambda ( x ) (nth 2 x)) lst) ("c1" nil "c3" "c4") Quote
rlx Posted September 26, 2017 Posted September 26, 2017 Just be aware of the following subtlety to the transpose approach:_$ (setq lst '(("a1" "b1" "c1" "d1") ("a2" "b2") ("a3" "b3" "c3") ("a4" "b4" "c4" "d4"))) (("a1" "b1" "c1" "d1") ("a2" "b2") ("a3" "b3" "c3") ("a4" "b4" "c4" "d4")) _$ (apply 'mapcar (cons 'list lst)) (("a1" "a2" "a3" "a4") ("b1" "b2" "b3" "b4")) _$ (nth 2 (apply 'mapcar (cons 'list lst))) nil _$ (mapcar '(lambda ( x ) (nth 2 x)) lst) ("c1" nil "c3" "c4") Ok , understood (emergency brain cell cooling system initiated) Quote
svorgodne Posted September 26, 2017 Author Posted September 26, 2017 Thanks Lee... I think I have not been able to make my point. My fault... I'll give it a try once again. I create layers for new drawings with a lisp routine with information taken from a CSV file. This file has the color code divided by RGB format. I managed to make exeptions for all 255 basic AutoCAD colors by filtering the code and giving instead the RGB code. Otherwise, color number, 7 or white, would appear as 255,255,255 since this is the RGB code for white. Assigning a color to a layer has three possibilities: New Color (1-255) Truecolor, which is a new step in between and would only take the RGB code divided by "," and Colorbook, dependant from a Pantone code. The first one would only take one value and could keep on going with the program. ("c" "100") The second would have to add another input ("c" "t" "126,117,201") The third one is much more complicated but also has even more steps than the other ones. I managed to make a list of colors of the layers I have as standards and also managed to exclude the combinations from all 255 basic AutoCAD colors, therefore, 255,255,255 is 7. The upper level of the routine assign all other values to the layers, like linetypes, plotstyles, on/off condition, thaw/frozen condition, lock/unlock... Yet the color property is to be specially treated given the situations I pointed in the previous paragraph (7 vs. 255,255,255) I also managed to filter this conditions and made a list of all existing colors in my standard file. The problem comes when running the routine like (command "-layer" ; partial list from list of lists, which contains for example the values ; ("c" "185") "c" "185" "" ) I want this information to be extracted from this partial list, because of course, there would be cases where the color code is not one of the basic AutoCAD colors and then the information taken from another paartial list would be (command "-layer" ; partial list from list of lists, which contains for example the values ; ("c" "t" "126,117,201") "c" "t" "126,117,201" "" ) In both cases, both partial lists ara part of a list of lists but cannot be run equally because one is having one step more "t". That's the reason I thought it would be possible to get and run this information as arguments which would probably be part of one function. Unfortunately, English is not my mother language and I cannot find any other way to explain it clearer. I hope you can really help me. Thanks again in advance. Quote
BIGAL Posted September 27, 2017 Posted September 27, 2017 (edited) A simple test (length lst) (strlength "126,117,201")) is 11 (strlength "100")) is 3 so use an if (if (> (strlen colstr) 3) (command "-layer" "c" "_t" "126,117,201" "") (command "-layer" "c" 185 "") ) Edited September 29, 2017 by BIGAL Quote
svorgodne Posted September 27, 2017 Author Posted September 27, 2017 A simple test (length lst) (strlength "126,117,201")) is 11 (strlength "100")) is 3 so use an if (if (> (strlength colstr) 3) (command "-layer" "c" "_t" "126,117,201" "") (command "-layer" "c" 185 "") ) Thank you very much Bigal. I do not intend to do it manually because it is a task I have to do probably two times per hour, ^with a list of almost 200 layers. I still think I have not made myself clear enough. Color Values are taken from an CSV table, and sometimes the value is less than 11 characters. For example, color 1 is "255,0,0". The values for each layer are taken directly from a CSV file with autolisp command "read-line", so it creates inside a list of all values for each layer. IF RGB color code for one layer is within the 255 basic AutoCAD colors, my routine converts it to this number instead of "R,G,B". The color list, already recognizes if it is a "number value" or a "R,G,B value", and it is saved in another list with the prefix "c" (for numbers) and "c" "t" (for RGB values). This values should be transfered as arguments to the routine that starts at some point like this: (command "-layer" (nth 0 PartialList) (nth 1 PartialList) ; and only if it is a RGB color (nth 2 PartialList) "" ) As you can see, it is slightly more complicated and that was the reason for me to ask the forum. Thank you very much for your time. Quote
hanhphuc Posted September 27, 2017 Posted September 27, 2017 i thinks MR has got the clue please look at post#10 but not sure not OP wanted? (setq PartialList '("c" "t" "255,255,255")) (apply 'vl-cmdf (cons "_.-layer" (append PartialList '("" "")))) or may try cond (>([color="blue"]length[/color] PartialList)2) or ([color="blue"]vl-some[/color] ''((x)(wcmatch x "#*`,*#*`,#")) PartialList) many possibilities? Quote
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.