Code Red > AutoLISP (Vanilla / Visual)

member function with lists in lists

(1/3) > >>

Amsterdammed:
Hello there,
i have  some list built up and i want add list to this one , but only if they are not allready member of the list. So i want to check out with the member function.

But why does
--- Code: ---
(if (not (member xfs axfs))
     (setq axfs (append axfs (list xfs))))




--- End code ---


give nil if xfs =
--- Code: ---
 (150.0 250.0 400.0 500.0 735.0 1250.0)



--- End code ---

and axfs =

--- Code: ---((735.0) (150.0 250.0 400.0 500.0 735.0 1250.0))



--- End code ---
?
 What do i overlook?

Thanks in Advance,
Bernd

MP:
If item order doesn't matter perhaps --


--- Code: ---(defun MergeLists ( list1 list2 / _MergeLists )
    (defun _MergeLists ( list1 list2 )
        (foreach item list1
            (if (null (member item list2))
                (setq list2 (cons item list2))
            )
        )
        list2
    )
    (if (<= (length list1) (length list2))
        (_MergeLists list1 list2)
        (_MergeLists list2 list1)
    )
)
--- End code ---

Example --

(MergeLists '(1 2 3) '(4 5 6))  >>>  (3 2 1 4 5 6)

If item order matters perhaps --


--- Code: ---(defun MergeLists ( list1 list2 / _MergeLists )
    (defun _MergeLists ( list1 list2 )
        (foreach item (reverse list1)
            (if (null (member item list2))
                (setq list2 (cons item list2))
            )
        )
        list2
    )
    (if (<= (length list1) (length list2))
        (_MergeLists list1 list2)
        (_MergeLists list2 list1)
    )
)
--- End code ---

Example --

(MergeLists '(1 2 3) '(4 5 6))  >>>  (1 2 3 4 5 6)

MP:
Given that you may have n number of lists to merge --


--- Code: ---(defun MergeLists ( lists / _MergeLists )
    (defun _MergeLists ( list1 list2 )
        (append       
            (vl-remove-if
               '(lambda (item) (member item list2))
                list1
            )
            list2
        )   
    )
    (   (lambda ( result )
            (foreach lst (cdr lists)
                (setq result
                    (if (<= (length lst) (length result))
                        (_MergeLists lst result)
                        (_MergeLists result lst)
                    )
                )   
            )   
        )
        (car (setq lists (reverse lists)))
    )       
)
--- End code ---


--- Code: ---(MergeLists
   '(
        (1 2 3)
        (1 2 3)
        (4 5 6)
    )
)
--- End code ---

>>> (1 2 3 4 5 6)

Note: Do not quote existing lists. i.e.


--- Code: ---(setq
    list1 '(1 2 3)
    list2 '(A B C)
)
--- End code ---

Correct: (MergeLists (list list1 list2)) >>> (1 2 3 A B C)

Wrong: (MergeLists '(list1 list2)) >>> Error

Edit: If you are h3ll bent on using '(list1 list2) because you think it looks cleaner you could always use mapcar / eval --

(MergeLists (mapcar 'eval '(list1 list2))) >>> (1 2 3 A B C)

But I wouldn't go that route myself.

MP:
Hey Bernd --

If this is all overkill to you just  use the core technique --

Set some data --


--- Code: ---(setq
    list1 '(1 2 3 4 5)
    list2 '(4 5 6 7 8)
)
--- End code ---

Use it --


--- Code: ---(setq result
    (append       
        (vl-remove-if
           '(lambda (item) (member item list2))
            list1
        )
        list2
    )
)
--- End code ---

Amsterdammed:
Thanks Michael,

Sorry for the late response but i was out biking in the afternoon.

I need the list as lists in a list. They form a pattern for distances on a bracket channel (piping and ducting), and i have plenty of them in a  drawing and i need to count the ones who are the same. (Sounds weired ,i know.) So i want to have all the different situations in a dwg as list in a list, so that i can than check all channels later in the code. But i don't want the same list twice, that is why i wanted to check with member.

Bernd

Navigation

[0] Message Index

[#] Next page

Go to full version