### Author Topic: Help List  (Read 6243 times)

0 Members and 1 Guest are viewing this topic.

#### velasquez

• Newt
• Posts: 195
##### Re: Help List
« Reply #15 on: July 30, 2014, 05:47:20 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))))))

I'm trying to sort a list by its second element.
The first element is always variable.
Laboring with the result vl-sort not working.
Lee Please can you tell me what is wrong?

Code: [Select]
`(setq l '(          ("-" "TNATJGS" "Tê com bolsas e junta elástica JGS - Linha Natural" "80" "80" "0" "0")          ("-" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0")          ("-" "TNATJGS" "Tê com bolsas e junta elástica JGS - Linha Natural" "80" "80" "0" "0")          ("C" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0")          ("-" "KB-BLUTOPTIPECB" "Cap JTI - Sistema Blutop" "160" "0" "0" "0")          ("-" "CC90B-BLUTOPPECB" "Curva 90° com bolsas JE - Sistema Blutop" "90" "0" "0" "0")          ("-" "TBLUTOP" "Tubo ponta e bolsa JE - Sistema Blutop" "90" "0" "0" "2000")          ("5" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0")          ("-" "CC22B-BLUTOPPECB" "Curva 22°30' com bolsas JE - Sistema Blutop" "90" "0" "0" "0")         ) ;_ fim de list)(vl-sort l '(lambda (a b) (eq (cadr a) (cadr b))))`

#### ronjonp

• Needs a day job
• Posts: 7535
##### Re: Help List
« Reply #16 on: July 30, 2014, 06:35:29 PM »
Try this:
Code: [Select]
`(vl-sort l '(lambda (a b) (< (cadr a) (cadr b))))`

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

#### velasquez

• Newt
• Posts: 195
##### Re: Help List
« Reply #17 on: July 30, 2014, 07:13:41 PM »
Try this:
Code: [Select]
`(vl-sort l '(lambda (a b) (< (cadr a) (cadr b))))`

Thanks ronjonp I used your suggestion in a function to count the items in a list.
Can you tell me how I can improve my way?
Code: [Select]
`(defun JoyFinalizaLista (lst / n ent wrt)  (while (> (length lst) 0)    (setq ent (car lst)          n   0    ) ;_ fim de setq    (while (and (car lst)                (eq (cadr (car lst)) (cadr ent))           ) ;_ fim de and      (setq n (1+ n))      (setq lst (cdr lst))    ) ;_ fim de while    (setq wrt (cons (append ent (list n)) wrt))  ) ;_ fim de while  wrt);;;Result -> '(("-" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0" 3) ("-" "TNATJGS" "Tê com bolsas e junta elástica JGS - Linha Natural" "80" "80" "0" "0" 2) ("-" "TBLUTOP" "Tubo ponta e bolsa JE - Sistema Blutop" "90" "0" "0" "2000" 1) ("-" "KB-BLUTOPTIPECB" "Cap JTI - Sistema Blutop" "160" "0" "0" "0" 1) ("-" "CC90B-BLUTOPPECB" "Curva 90° com bolsas JE - Sistema Blutop" "90" "0" "0" "0" 1) ("-" "CC22B-BLUTOPPECB" "Curva 22°30' com bolsas JE - Sistema Blutop" "90" "0" "0" "0" 1))`

#### CAB

• Global Moderator
• Seagull
• Posts: 10401
##### Re: Help List
« Reply #18 on: July 30, 2014, 09:50:21 PM »
Did you try this as suggested by Ron?
Code: [Select]
`(defun JoyFinalizaLista (l)  (vl-sort l '(lambda (a b) (< (cadr a) (cadr b)))))`
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970

#### velasquez

• Newt
• Posts: 195
##### Re: Help List
« Reply #19 on: July 31, 2014, 07:08:45 AM »
Did you try this as suggested by Ron?
Code: [Select]
`(defun JoyFinalizaLista (l)  (vl-sort l '(lambda (a b) (< (cadr a) (cadr b)))))`

Hello CAB,
Ron's suggestion worked perfectly.
I failed my goal when I showed her.
My question is the way that I showed to count the elements in the list can be improved.
I corrected my code.
Code: [Select]
`(setq l '(          ("-" "TNATJGS" "Tê com bolsas e junta elástica JGS - Linha Natural" "80" "80" "0" "0")          ("-" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0")          ("-" "TNATJGS" "Tê com bolsas e junta elástica JGS - Linha Natural" "80" "80" "0" "0")          ("C" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0")          ("-" "KB-BLUTOPTIPECB" "Cap JTI - Sistema Blutop" "160" "0" "0" "0")          ("-" "TBLUTOP" "Tubo ponta e bolsa JE - Sistema Blutop" "90" "0" "0" "2000")          ("5" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0")          ("-" "CC90B-BLUTOPPECB" "Curva 90° com bolsas JE - Sistema Blutop" "90" "0" "0" "0")          ("-" "CC22B-BLUTOPPECB" "Curva 22°30' com bolsas JE - Sistema Blutop" "90" "0" "0" "0")         ) ;_ fim de list);;;(defun JoyFinalizaLista (lst / lst n ent wrt);;;BY RONJONP(setq lst (vl-sort lst '(lambda (a b) (< (cadr a) (cadr b)))));;;  (while (> (length lst) 0)    (setq ent (car lst)          n   0    ) ;_ fim de setq    (while (and (car lst)                (eq (cadr (car lst)) (cadr ent))           ) ;_ fim de and      (setq n (1+ n))      (setq lst (cdr lst))    ) ;_ fim de while    (setq wrt (cons (append ent (list n)) wrt))  ) ;_ fim de while  wrt);;;TEST_\$ (JoyFinalizaLista l)(("-" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0" 3) ("-" "TNATJGS" "Tê com bolsas e junta elástica JGS - Linha Natural" "80" "80" "0" "0" 2) ("-" "TBLUTOP" "Tubo ponta e bolsa JE - Sistema Blutop" "90" "0" "0" "2000" 1) ("-" "KB-BLUTOPTIPECB" "Cap JTI - Sistema Blutop" "160" "0" "0" "0" 1) ("-" "CC90B-BLUTOPPECB" "Curva 90° com bolsas JE - Sistema Blutop" "90" "0" "0" "0" 1) ("-" "CC22B-BLUTOPPECB" "Curva 22°30' com bolsas JE - Sistema Blutop" "90" "0" "0" "0" 1))_\$ `

#### CAB

• Global Moderator
• Seagull
• Posts: 10401
##### Re: Help List
« Reply #20 on: July 31, 2014, 08:44:28 AM »
I don't understand what you are counting?
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970

#### velasquez

• Newt
• Posts: 195
##### Re: Help List
« Reply #21 on: July 31, 2014, 01:10:58 PM »
I don't understand what you are counting?

I'm counting the second item of each list (cadr ..) and adding the value at the end of the list.
I used the idea of ​​Ron to sort the list.
Please exeucte my code and observe the returned list.

#### ronjonp

• Needs a day job
• Posts: 7535
##### Re: Help List
« Reply #22 on: July 31, 2014, 02:05:49 PM »
Give this a try:

Code: [Select]
`(setq  l '(("-" "TNATJGS" "Tê com bolsas e junta elástica JGS - Linha Natural" "80" "80" "0" "0")      ("-" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0")      ("-" "TNATJGS" "Tê com bolsas e junta elástica JGS - Linha Natural" "80" "80" "0" "0")      ("C" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0")      ("-" "KB-BLUTOPTIPECB" "Cap JTI - Sistema Blutop" "160" "0" "0" "0")      ("-" "CC90B-BLUTOPPECB" "Curva 90° com bolsas JE - Sistema Blutop" "90" "0" "0" "0")      ("-" "TBLUTOP" "Tubo ponta e bolsa JE - Sistema Blutop" "90" "0" "0" "2000")      ("5" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0")      ("-" "CC22B-BLUTOPPECB" "Curva 22°30' com bolsas JE - Sistema Blutop" "90" "0" "0" "0")     ) ;_ fim de list)(mapcar  (function    (lambda (x)      (append x (list (length (vl-remove-if-not (function (lambda (txt) (= (cadr x) txt))) (mapcar 'cadr l))) )      )    )  )  l)`
« Last Edit: July 31, 2014, 02:42:47 PM by ronjonp »

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

#### velasquez

• Newt
• Posts: 195
##### Re: Help List
« Reply #23 on: July 31, 2014, 03:04:29 PM »
Give this a try:

Code: [Select]
`(setq  l '(("-" "TNATJGS" "Tê com bolsas e junta elástica JGS - Linha Natural" "80" "80" "0" "0")      ("-" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0")      ("-" "TNATJGS" "Tê com bolsas e junta elástica JGS - Linha Natural" "80" "80" "0" "0")      ("C" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0")      ("-" "KB-BLUTOPTIPECB" "Cap JTI - Sistema Blutop" "160" "0" "0" "0")      ("-" "CC90B-BLUTOPPECB" "Curva 90° com bolsas JE - Sistema Blutop" "90" "0" "0" "0")      ("-" "TBLUTOP" "Tubo ponta e bolsa JE - Sistema Blutop" "90" "0" "0" "2000")      ("5" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0")      ("-" "CC22B-BLUTOPPECB" "Curva 22°30' com bolsas JE - Sistema Blutop" "90" "0" "0" "0")     ) ;_ fim de list)(mapcar  (function    (lambda (x)      (append x (list (length (vl-remove-if-not (function (lambda (txt) (= (cadr x) txt))) (mapcar 'cadr l))) )      )    )  )  l)`
Hi Ron,
Your code did the count I need.
But the list of return must be in the format below.
I'll study it.
Thank you very much.
Code: [Select]
`(finalList '(("-" "VBFWCV10" "Válvula borboleta com flanges e volante na posição 1" "200" "0" "10" "0" 3) ("-" "TNATJGS" "Tê com bolsas e junta elástica JGS - Linha Natural" "80" "80" "0" "0" 2) ("-" "TBLUTOP" "Tubo ponta e bolsa JE - Sistema Blutop" "90" "0" "0" "2000" 1) ("-" "KB-BLUTOPTIPECB" "Cap JTI - Sistema Blutop" "160" "0" "0" "0" 1) ("-" "CC90B-BLUTOPPECB" "Curva 90° com bolsas JE - Sistema Blutop" "90" "0" "0" "0" 1) ("-" "CC22B-BLUTOPPECB" "Curva 22°30' com bolsas JE - Sistema Blutop" "90" "0" "0" "0" 1)))`

#### ronjonp

• Needs a day job
• Posts: 7535
##### Re: Help List
« Reply #24 on: July 31, 2014, 03:49:22 PM »
Give this a whirl:

Code: [Select]
`(while (setq line (car l))  (setq tmp (vl-remove-if-not (function (lambda (x) (= (cadr line) (cadr x)))) l))  (setq result (cons (append line (list (length tmp))) result))  (mapcar (function (lambda (x) (setq l (vl-remove x l)))) tmp))(setq result (vl-sort result '(lambda (a b) (< (cadr a) (cadr b)))))`

Windows 11 x64 - AutoCAD /C3D 2023

Custom Build PC

#### Lee Mac

• Seagull
• Posts: 12943
• London, England
##### Re: Help List
« Reply #25 on: July 31, 2014, 04:51:07 PM »
Another variation:

Code - Auto/Visual Lisp: [Select]
1. (defun foo ( l / a r )
2.     (foreach x l
3.         (if (setq a (assoc (cadr x) r))
4.             (setq r (subst (subst (1+ (last a)) (last a) a) a r))
5.             (setq r (cons  (append (cons (cadr x) x) '(1)) r))
6.         )
7.     )
8.     (vl-sort (mapcar 'cdr r) '(lambda (a b) (> (cadr a) (cadr b))))
9. )

#### Lee Mac

• Seagull
• Posts: 12943
• London, England
##### Re: Help List
« Reply #26 on: July 31, 2014, 04:53:50 PM »
And another:

Code - Auto/Visual Lisp: [Select]
1. (defun foo ( l / n r x )
2.     (while l
3.         (setq x (car l)
4.               n (length l)
5.               l (vl-remove-if '(lambda ( y ) (= (cadr x) (cadr y))) (cdr l))
6.               r (cons (append x (list (- n (length l)))) r)
7.         )
8.     )