Author Topic: Help List  (Read 5810 times)

0 Members and 1 Guest are viewing this topic.

velasquez

  • Newt
  • Posts: 195
Help List
« on: July 18, 2014, 03:34:03 PM »
I need help for a problem with lists:

I have:
list1: ->
Code: [Select]
(setq list1 (list '("1" "testA") '("2" "testB") '("3" "testC")))and
list2: ->
Code: [Select]
(setq list2 (list '("testA" "nononno") '("testB" "vcvcvcv") '("testC" "nmnmnmn") '("nonono" "xccxcxc") '("xxxxxx" "jhjhjhjj")))I need to get list3 ->
Code: [Select]
'(("1" "teste
A" "aaaaaa") ("2" "testB" "vcvcvcv") ("3" "testC" "nmnmnmn") ("" "nonono" "xccxcxc") ("" "xxxxxx" "jhjhjhjj"))

Can anyone help me?

Thanks

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Help List
« Reply #1 on: July 18, 2014, 04:05:42 PM »
Quick one:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( l1 l2 )
  2.     (setq l1 (mapcar 'reverse l1))
  3.     (mapcar '(lambda ( x / a ) (if (setq a (assoc (car x) l1)) (reverse (cons (cadr x) a)) (cons "" x))) l2)
  4. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (foo list1 list2)
  2. (("1" "testA" "nononno") ("2" "testB" "vcvcvcv") ("3" "testC" "nmnmnmn") ("" "nonono" "xccxcxc") ("" "xxxxxx" "jhjhjhjj"))

Actually, this is slightly shorter:
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( l1 l2 )
  2.     (setq l1 (mapcar 'reverse l1))
  3.     (mapcar '(lambda ( x / a ) (if (setq a (assoc (car x) l1)) (cons (cadr a) x) (cons "" x))) l2)
  4. )
« Last Edit: July 18, 2014, 04:10:37 PM by Lee Mac »

velasquez

  • Newt
  • Posts: 195
Re: Help List
« Reply #2 on: July 18, 2014, 04:36:27 PM »
Thank you Lee
Your help was very important again.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Help List
« Reply #3 on: July 19, 2014, 05:28:48 AM »
You're welcome velasquez  :-)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Help List
« Reply #4 on: July 19, 2014, 05:32:43 AM »
Another, recursive variation (slow):

Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( a b )
  2.     (if b (cons (bar (car b) a) (foo a (cdr b))))
  3. )
  4. (defun bar ( x l )
  5.     (cond
  6.         (   (null l) (cons "" x))
  7.         (   (= (cadar l) (car x)) (cons (caar l) x))
  8.         (   (bar x (cdr l)))
  9.     )
  10. )

Code - Auto/Visual Lisp: [Select]
  1. _$ (foo list1 list2)
  2. (("1" "testA" "nononno") ("2" "testB" "vcvcvcv") ("3" "testC" "nmnmnmn") ("" "nonono" "xccxcxc") ("" "xxxxxx" "jhjhjhjj"))

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Help List
« Reply #5 on: July 19, 2014, 06:56:32 AM »
or a little simpler way would:
Code - Auto/Visual Lisp: [Select]
  1. (setq list3 nil)
  2. (foreach a list1
  3.   (setq list3 (cons (list (car a)
  4.                           (cadr a)
  5.                           (cadr (assoc (cadr a) list2))) list3)))
  6. (setq list3 (reverse list3))
  7.  

No error checking though which I would recommend  -David
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Help List
« Reply #6 on: July 19, 2014, 07:56:55 AM »
or a little simpler way would:
Code - Auto/Visual Lisp: [Select]
  1. (setq list3 nil)
  2. (foreach a list1
  3.   (setq list3 (cons (list (car a)
  4.                           (cadr a)
  5.                           (cadr (assoc (cadr a) list2))) list3)))
  6. (setq list3 (reverse list3))
  7.  

What about these items David:
Code - Auto/Visual Lisp: [Select]
  1. ("" "nonono" "xccxcxc") ("" "xxxxxx" "jhjhjhjj")

:wink:

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Help List
« Reply #7 on: July 19, 2014, 10:44:57 AM »

What about these items David:
Code - Auto/Visual Lisp: [Select]
  1. ("" "nonono" "xccxcxc") ("" "xxxxxx" "jhjhjhjj")

:wink:

Like I said  it needs error checking.  I don't believe there would be any correct answer to your scenario.  My $0.02  -David
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Help List
« Reply #8 on: July 19, 2014, 12:25:37 PM »

What about these items David:
Code - Auto/Visual Lisp: [Select]
  1. ("" "nonono" "xccxcxc") ("" "xxxxxx" "jhjhjhjj")

:wink:

Like I said  it needs error checking.  I don't believe there would be any correct answer to your scenario.  My $0.02  -David

I'm just going by the result requested by the OP.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Help List
« Reply #9 on: July 19, 2014, 03:55:44 PM »
Just a question: Are the 2 lists always in the same order? If so a slightly faster version:
Code - Auto/Visual Lisp: [Select]
  1. (defun combine (L1 L2 / L3)
  2.   (setq L3 L2)
  3.   (append (mapcar '(lambda (a b) (setq L3 (cdr L3)) (cons (car a) b))) L1 L2)
  4.           (mapcar '(lambda (c) (cons "" c)) L3)))
Else you'll need the assoc to extract the correct item from either L1 or L2 (as the others' are doing).

BTW, is the 2nd list always longer (or same length) as the first or could the 1st list be longer? If so how should the result then look?
« Last Edit: July 19, 2014, 03:59:41 PM by irneb »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

velasquez

  • Newt
  • Posts: 195
Re: Help List
« Reply #10 on: July 25, 2014, 07:52:14 PM »
You're welcome velasquez  :-)
Hi Lee,
I modified your function foo and got the list3 as follows:
Code: [Select]
(defun LM:foo+ (l1 l2)
  (setq l1 (mapcar 'reverse l1))
  (mapcar '(lambda (x / a)
             (if (setq a (assoc (car x) l1))
               (subst (cadr a) (car x) x)
               (subst "" (car x) x)
             ) ;_ fim de if
           ) ;_ fim de lambda
          l2
  ) ;_ fim de mapcar
) ;_ fim de defun
Code: [Select]
(setq list1 '(("1" "SG_1_TBLUTOP_D90_L2000") ("2" "SG_3_CP90FB-BLUTOP10PECB_D90") ("3" "SG_1_CC90B-BLUTOPPECB_D90")))
(setq list2 '(("SG_1_CC90B-BLUTOPPECB_D90" "CC90B-BLUTOPPECB;Curva 90° com bolsas JE - Sistema Blutop;90;0;0;0") ("SG_3_CP90FB-BLUTOP10PECB_D90" "CP90FB-BLUTOP10/16PECB;Curva 90° com pé, bolsa e flange JE - Sistema Blutop;80;80;10;0") ("SG_1_TBLUTOP_D90_L2000" "TBLUTOP;Tubo ponta e bolsa JE - Sistema Blutop;90;0;0;2000") ("SG_1_KB-BLUTOPPECB_D90" "KB-BLUTOPPECB;Cap JE - Sistema Blutop;90;0;0;0") ("SG_1_KB-BLUTOPTIPECB_D160" "KB-BLUTOPTIPECB;Cap JTI - Sistema Blutop;160;0;0;0")))
 
I got the list3 with this order:
Code: [Select]
_$ (setq list3 (LM:FOO+ list1 list2))
(("3" "CC90B-BLUTOPPECB;Curva 90° com bolsas JE - Sistema Blutop;90;0;0;0") ("2" "CP90FB-BLUTOP10/16PECB;Curva 90° com pé, bolsa e flange JE - Sistema Blutop;80;80;10;0") ("1" "TBLUTOP;Tubo ponta e bolsa JE - Sistema Blutop;90;0;0;2000") ("" "KB-BLUTOPPECB;Cap JE - Sistema Blutop;90;0;0;0") ("" "KB-BLUTOPTIPECB;Cap JTI - Sistema Blutop;160;0;0;0"))
_$
You can help me sort the list as list4:?
Code: [Select]
'(("1" "TBLUTOP;Tubo ponta e bolsa JE - Sistema Blutop;90;0;0;2000")
  ("2" "CP90FB-BLUTOP10/16PECB;Curva 90° com pé, bolsa e flange JE - Sistema Blutop;80;80;10;0")
  ("3" "CC90B-BLUTOPPECB;Curva 90° com bolsas JE - Sistema Blutop;90;0;0;0")
  ("" "KB-BLUTOPPECB;Cap JE - Sistema Blutop;90;0;0;0")
  ("" "KB-BLUTOPTIPECB;Cap JTI - Sistema Blutop;160;0;0;0"))

Thanks for the time and the help of all.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Help List
« Reply #11 on: July 25, 2014, 08:00:07 PM »
Quick one:
Code - Auto/Visual Lisp: [Select]
  1. (setq l
  2.    '(
  3.         ("3" "CC90B-BLUTOPPECB;Curva 90° com bolsas JE - Sistema Blutop;90;0;0;0")
  4.         ("2" "CP90FB-BLUTOP10/16PECB;Curva 90° com pé, bolsa e flange JE - Sistema Blutop;80;80;10;0")
  5.         ("1" "TBLUTOP;Tubo ponta e bolsa JE - Sistema Blutop;90;0;0;2000")
  6.         ("" "KB-BLUTOPPECB;Cap JE - Sistema Blutop;90;0;0;0")
  7.         ("" "KB-BLUTOPTIPECB;Cap JTI - Sistema Blutop;160;0;0;0")
  8.     )
  9. )
  10.  
  11. (vl-sort l '(lambda ( a b ) (if (and (/= "" (car a)) (/= "" (car b))) (< (atoi (car a)) (atoi (car b))))))

velasquez

  • Newt
  • Posts: 195
Re: Help List
« Reply #12 on: July 26, 2014, 06:09:26 AM »
Quick one:
Code - Auto/Visual Lisp: [Select]
  1. (setq l
  2.    '(
  3.         ("3" "CC90B-BLUTOPPECB;Curva 90° com bolsas JE - Sistema Blutop;90;0;0;0")
  4.         ("2" "CP90FB-BLUTOP10/16PECB;Curva 90° com pé, bolsa e flange JE - Sistema Blutop;80;80;10;0")
  5.         ("1" "TBLUTOP;Tubo ponta e bolsa JE - Sistema Blutop;90;0;0;2000")
  6.         ("" "KB-BLUTOPPECB;Cap JE - Sistema Blutop;90;0;0;0")
  7.         ("" "KB-BLUTOPTIPECB;Cap JTI - Sistema Blutop;160;0;0;0")
  8.     )
  9. )
  10.  
  11. (vl-sort l '(lambda ( a b ) (if (and (/= "" (car a)) (/= "" (car b))) (< (atoi (car a)) (atoi (car b))))))

Perfect Lee.
Thanks

velasquez

  • Newt
  • Posts: 195
Re: Help List
« Reply #13 on: July 28, 2014, 09:22:57 AM »
I'm trying to better understand the mapcar and lambda functions.
I got a result with foreach but I can not say with mapcar.
Code: [Select]
;;;MyList -> (<Entity name: 7ffffc08530> <Entity name: 7ffffc05960> <Entity name: 7ffffc05440> ...)
(foreach JoyItem MyList
  (if (setq JoyTemp (dxf 1000 (JoyGetXdata (vlax-ename->vla-object JoyItem) "DuctilCAD_2D")))
    (setq JoyData (cons (list (dxf 2 (entget JoyItem)) JoyTemp) JoyData))
  ) ;_ fim de if
) ;_ fim de foreach

;;;returned list -> '(("SG_1_TNATJGS_D80_#D80" "TNATJGS;Tê com bolsas e junta elástica JGS - Linha Natural;80;80;0;0")
  ("SG_1_KB-BLUTOPTIPECB_D160" "KB-BLUTOPTIPECB;Cap JTI - Sistema Blutop;160;0;0;0")
  ("SG_1_TNATJGS_D80_#D80" "TNATJGS;Tê com bolsas e junta elástica JGS - Linha Natural;80;80;0;0")
  ("SG_1_KB-BLUTOPTIPECB_D160" "KB-BLUTOPTIPECB;Cap JTI - Sistema Blutop;160;0;0;0")
)
My question in this case is better to work with foreach?
I need to learn more about mapcar.

Thanks

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Help List
« Reply #14 on: July 28, 2014, 10:18:38 AM »
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.