Jump to content

Compare multiple lists to make new list


DGRL

Recommended Posts

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

Link to comment
Share on other sites

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

)

Link to comment
Share on other sites

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))

)

Link to comment
Share on other sites

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)) 
       ) 
     )
   )
 )
)

Link to comment
Share on other sites

@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 :thumbsup:

 

 

 

 

BTW

Where can I find more info about the MAPCAR and LAMBDA?

Seems interesting to use this

Link to comment
Share on other sites

@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 :thumbsup:

 

 

Cheers :beer:

 

 

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))
)

Link to comment
Share on other sites

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...