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

Making a new list.

13 REPLIES 13
SOLVED
Reply
Message 1 of 14
Luís Augusto
551 Views, 13 Replies

Making a new list.

Hello everybody.
I'm having trouble generating a new list.

I have a list with the following configuration.

 

(
	("3333" "AA" "PIN_1" "01 PR 18AWG")
	("3333" "AA" "PIN_2" "06 AM 18AWG")
	("3333" "AA" "PIN_3" "11 VM 18AWG")
	("3333" "BB" "PIN_1" "01 PR 18AWG")
	("3333" "BB" "PIN_2" "06 AM 18AWG")
	("3333" "BB" "PIN_3" "11 VM 18AWG")
)

 

 I would create a new list containing two new lists. What will determine the formation (grouping) of a new list is the cadr of each sub list.

Could show me how I could do this?
The results would be as shown below.

 

(
	(
	("3333" "AA" "PIN_1" "01 PR 18AWG")
	("3333" "AA" "PIN_2" "06 AM 18AWG")
	("3333" "AA" "PIN_3" "11 VM 18AWG")
	)
	
	(
	("3333" "BB" "PIN_1" "01 PR 18AWG")
	("3333" "BB" "PIN_2" "06 AM 18AWG")
	("3333" "BB" "PIN_3" "11 VM 18AWG")
	)
)

 Any help will be appreciated.

 

Best Regards,

Luís Augusto Silva.

13 REPLIES 13
Message 2 of 14

Take a look at VL-REMOVE-IF and VL-REMOVE-IF-NOT.

 

--

 

Message 3 of 14

Many thanks for the reply.
I think this will solve my case.
If it is not asking too much, could you give me an example of a loop with this function?
I'm still learning and I have some limitations.

 

Thanks.

Message 4 of 14
_Tharwat
in reply to: Luís Augusto

Hi Luis .

 

This is an example to check if the second element is  equal to string "BB" , then it would be gathered in a list .

 

(setq l '(
          ("3333" "AA" "PIN_1" "01 PR 18AWG")
          ("3333" "AA" "PIN_2" "06 AM 18AWG")
          ("3333" "AA" "PIN_3" "11 VM 18AWG")
          ("3333" "BB" "PIN_1" "01 PR 18AWG")
          ("3333" "BB" "PIN_2" "06 AM 18AWG")
          ("3333" "BB" "PIN_3" "11 VM 18AWG")
         )
)
(setq l1 (vl-remove-if '(lambda (u) (eq (cadr u) "BB")) l))

 

Message 5 of 14
dbroad
in reply to: Luís Augusto

One approach, assuming that you have more than two types of pins:

(defun regroup ( key keys lst)
  (if (or (null keys)(null lst))
    nil
  (cons (vl-remove-if-not '(lambda (x) (= (key x)(car keys))) lst)
	(regroup key (cdr keys) (vl-remove-if '(lambda (x) (= (key x) (car keys))) lst)))))

 Using that function and assuming your first list is stored in the symbol LST, then 

(regroup cadr '("AA""BB""CC""DD") lst) would return a list grouped into up to four parts based on the cadr of each sublist where cadr is the key.  The equal or eq functions might also be more appropriate than the = function where used above.

Architect, Registered NC, VA, SC, & GA.
Message 6 of 14
_gile
in reply to: dbroad

Hi,

 

A way usin a generic GrouBy function.

gc:GroupBy requires two arguments: a key generator function to group the items, and the list to process. It returns a list of sublists (groups) which each car is the key.

 

(defun gc:groupBy (fun lst / key sub res)
  (setq fun (eval fun))
  (mapcar
    '(lambda (l) (cons (car l) (reverse (cdr l))))
    (foreach l lst
      (setq res
	     (if (setq sub (assoc (setq key (fun l)) res))
	       (subst (vl-list* key l (cdr sub)) sub res)
	       (cons (list key l) res)
	     )
      )
    )
  )
)

 (gc:groupBy 'cadr l) returns:

(("BB" ("3333" "BB" "PIN_1" "01 PR 18AWG")
       ("3333" "BB" "PIN_2" "06 AM 18AWG")
       ("3333" "BB" "PIN_3" "11 VM 18AWG")
 )
  ("AA"	("3333" "AA" "PIN_1" "01 PR 18AWG")
	("3333" "AA" "PIN_2" "06 AM 18AWG")
	("3333" "AA" "PIN_3" "11 VM 18AWG")
  )
)

(mapcar 'cdr (gc:groupBy 'cadr l)) returns:

((("3333" "BB" "PIN_1" "01 PR 18AWG")
   ("3333" "BB" "PIN_2" "06 AM 18AWG")
   ("3333" "BB" "PIN_3" "11 VM 18AWG")
 )
  (("3333" "AA" "PIN_1" "01 PR 18AWG")
    ("3333" "AA" "PIN_2" "06 AM 18AWG")
    ("3333" "AA" "PIN_3" "11 VM 18AWG")
  )
)

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 7 of 14
Luís Augusto
in reply to: _Tharwat

Tharwat Hello, I am very happy to see you here.
Many thanks for the reply.

Do you recognize these lists?
I'm trying to make a change in the program for inserting blocks you wrote.
I have problems when I have the same codes. The function '(vla-put-TextString), is entering the information into a single connector.
To try to circumvent this problem, I put another column in the table that contains the information, in order to differentiate the positions.

If I can not solve, you could help me?
 
Grateful.

Luis Augusto
Message 8 of 14
_Tharwat
in reply to: Luís Augusto


@Luís Augusto wrote:
Tharwat Hello, I am very happy to see you here.
Many thanks for the reply.

Do you recognize these lists?
I'm trying to make a change in the program for inserting blocks you wrote.
I have problems when I have the same codes. The function '(vla-put-TextString), is entering the information into a single connector.
To try to circumvent this problem, I put another column in the table that contains the information, in order to differentiate the positions.

If I can not solve, you could help me?
 
Grateful.

Luis Augusto

With all pleasure , but I need to understand what you are after to be able to give the correct answer .

Message 9 of 14
dbroad
in reply to: _gile

Very nice Gile.  With mine, I would have needed to collect the keys first, as in

(defun findkeys (selector lst / key)
(if (null lst)
nil
(cons
(setq key (selector (car lst)))
(findkeys
selector
(vl-remove-if '(lambda (x) (= (selector x) key)) (cdr lst))
))))

 

So (regroup cadr (findkeys lst) lst)

 

Your method would execute faster.

Architect, Registered NC, VA, SC, & GA.
Message 10 of 14
_gile
in reply to: dbroad

Thanks Doug.

 

I don't play so much with LISP these times, but the last funny thing I did was implementing in AutoLISP some F# functions as groupBy, fold, unfold, take, skip...



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 11 of 14
dbroad
in reply to: Luís Augusto

Thanks for the update.  I'm here trying to keep from getting too rusty with LISP.  I'm doing most of my work in javascript (and some light php, and jquery) these days (moodle supported instructional apps). Hadn't even heard of the F# language. You'll have to fill me in more on another thread. 

Architect, Registered NC, VA, SC, & GA.
Message 12 of 14
_gile
in reply to: dbroad

F# is a.NET first class language (as C# or VB) that provides support for functional programming. Unfortunately it remains confidential (we are very few programming AutoCAD with it).

If you have some time to spend, you can see these videos:

An Introduction to Microsoft F#, by Luca Bolognese, not related to AutoCAD but very funny.

Developing for autoCAD using F#, by Kean Walmsley.

We could discuss deeper about this in the .NET forum.

 

Sorry for trolling this post.

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

Message 13 of 14
Luís Augusto
in reply to: dbroad

First, thanks to all who responded.
I felt honored to receive several replies. I'm definitely in the right place to learn.
Gile, the return of its function, is infinitely better than I expected. Many thanks to make available this gem to the community.
Tharwat, thank you for always being willing to help me. I am really thankful for that. To practice, I will try to make changes to the code. If I do not succeed in changing, I'll post the code with my questions, okay?
To members who have not mentioned the name, I must say that their codes and tips also have helped me. Each day learning a little more, thanks to answers from you.

Many thanks to all.

 

Best regards, Luís Augusto.

Message 14 of 14
_Tharwat
in reply to: Luís Augusto


@Luís Augusto wrote:


Tharwat, thank you for always being willing to help me. I am really thankful for that. To practice, I will try to make changes to the code. If I do not succeed in changing, I'll post the code with my questions, okay?

 

Best regards, Luís Augusto.


You're welcome any time Luis .

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

Post to forums  

Autodesk Design & Make Report

”Boost