TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: HasanCAD on March 21, 2019, 06:31:28 AM

Title: How to extract data from this list?
Post by: HasanCAD on March 21, 2019, 06:31:28 AM
Need some Help in extracting data from a list
I have this list
Code - Auto/Visual Lisp: [Select]
  1. (
  2.     ("cable01" "M1" "E1")
  3.     ("cable02" "M1" "E2")
  4.     ("cable03" "M1" "E3")
  5.     ("cable04" "E1" "M4")
  6.     ("cable05" "E2" "M5")
  7.     ("cable06" "E2" "M1")
  8. )

So How to extract cable# (first item in list ) if I asked for cable which connected to a machine E1
And sort if there is more than one cable.

For example I asked for E1
The lisp filter cables number in Mtext in raws (even in second or third item)
cable01, M1
cable04, M4

For example I asked for M1
cable01, E1
cable02, E2
cable06, E2
cable03, E3


Thanks in advance
Title: Re: How to extract data from this list?
Post by: kdub_nz on March 21, 2019, 06:42:07 AM
How are you currently getting the data results ? Post code ?

What result did you expect in the samples you have shown ?

Added:
Is the data format consistantly shaped ??
Title: Re: How to extract data from this list?
Post by: Marc'Antonio Alessi on March 21, 2019, 07:19:20 AM
Maybe rebuild the list is one option:
Code: [Select]
(setq #List
'(
    ("cable01" "M1" "E1")
    ("cable02" "M1" "E2")
    ("cable03" "M1" "E3")
    ("cable04" "E1" "M4")
    ("cable05" "E2" "M5")
    ("cable06" "E2" "M1")
))

(defun TestAssoc (KeyVal DatLst / AscLst)
  (foreach ForElm DatLst
    (setq AscLst (cons (cons (cadr ForElm) (car ForElm))   (cons (cons (caddr ForElm) (car ForElm)) AscLst)))
  )
  (Cdrs KeyVal AscLst)
)

; Orginal by M. Puckett  > Cdrs
(defun Cdrs (DxfKey ImpLst / TmpLst OutLst)
  (while (setq TmpLst (assoc DxfKey ImpLst))
    (setq OutLst (cons (cdr TmpLst) OutLst)
          ImpLst (cdr (member TmpLst ImpLst))
    )
  )
  (reverse OutLst)
)

(TestAssoc "E1" #List) =>("cable04" "cable01")
(TestAssoc "M1" #List) =>("cable06" "cable03" "cable02" "cable01")
Title: Re: How to extract data from this list?
Post by: kpblc on March 21, 2019, 07:38:22 AM
Maybe something like this?
Code - Auto/Visual Lisp: [Select]
  1. (defun t1 (data-list name / res)
  2.   (foreach item data-list
  3.     (cond ((= (cadr item) name) (setq res (cons (list (car item) (caddr item)) res)))
  4.           ((= (caddr item) name) (setq res (cons (list (car item) (cadr item)) res)))
  5.           ) ;_ end of cond
  6.     ) ;_ end of foreach
  7.   (reverse res)
  8.   ) ;_ end of defun
  9.  
Results are:
Code - Auto/Visual Lisp: [Select]
  1. _$ (t1 '(("cable01" "M1" "E1")
  2.             ("cable02" "M1" "E2")
  3.             ("cable03" "M1" "E3")
  4.             ("cable04" "E1" "M4")
  5.             ("cable05" "E2" "M5")
  6.             ("cable06" "E2" "M1")
  7.             )  "E2")
  8. (("cable02" "M1") ("cable05" "M5") ("cable06" "M1"))
  9. _$ (t1 '(("cable01" "M1" "E1")
  10.             ("cable02" "M1" "E2")
  11.             ("cable03" "M1" "E3")
  12.             ("cable04" "E1" "M4")
  13.             ("cable05" "E2" "M5")
  14.             ("cable06" "E2" "M1")
  15.             )  "E1")
  16. (("cable01" "M1") ("cable04" "M4"))
Title: Re: How to extract data from this list?
Post by: Grrr1337 on March 21, 2019, 08:42:37 AM
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( k aL )
  2.   (apply 'append (mapcar '(lambda (x) (if (member k (cdr x)) (list (vl-remove k x)))) aL))
  3. )

Code - Auto/Visual Lisp: [Select]
  1. ;|
  2. For example I asked for E1
  3. The lisp filter cables number in Mtext in raws (even in second or third item)
  4. cable01, M1
  5. cable04, M4
  6. |;
  7. (f "E1"
  8.   '(
  9.     ("cable01" "M1" "E1")
  10.     ("cable02" "M1" "E2")
  11.     ("cable03" "M1" "E3")
  12.     ("cable04" "E1" "M4")
  13.     ("cable05" "E2" "M5")
  14.     ("cable06" "E2" "M1")
  15.   )
  16. )
  17. >> (("cable01" "M1") ("cable04" "M4"))

Code - Auto/Visual Lisp: [Select]
  1. ;|
  2. For example I asked for M1
  3. cable01, E1
  4. cable02, E2
  5. cable06, E2
  6. cable03, E3
  7. |;
  8. (f "M1"
  9.   '(
  10.     ("cable01" "M1" "E1")
  11.     ("cable02" "M1" "E2")
  12.     ("cable03" "M1" "E3")
  13.     ("cable04" "E1" "M4")
  14.     ("cable05" "E2" "M5")
  15.     ("cable06" "E2" "M1")
  16.   )
  17. )
  18. >> (("cable01" "E1") ("cable02" "E2") ("cable03" "E3") ("cable06" "E2"))

Title: Re: How to extract data from this list?
Post by: Lee Mac on March 21, 2019, 09:14:48 AM
Another -
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( x l )
  2.     (mapcar '(lambda ( i ) (vl-remove x i)) (vl-remove-if-not '(lambda ( i ) (member x i)) l))
  3. )
Code - Auto/Visual Lisp: [Select]
  1. _$ (foo "M1" l)
  2. (("cable01" "E1") ("cable02" "E2") ("cable03" "E3") ("cable06" "E2"))
  3. _$ (foo "E1" l)
  4. (("cable01" "M1") ("cable04" "M4"))
Title: Re: How to extract data from this list?
Post by: HasanCAD on March 23, 2019, 06:58:18 AM
How are you currently getting the data results ? Post code ?
I imported from an Excel worksheet using http://www.lee-mac.com/readcsv.html
(http://)

What result did you expect in the samples you have shown ?

The result could be MText I can handle this issue.

Is the data format consistantly shaped ??
I did not get this point
What I want to do is
- Read CSV file I handled using http://www.lee-mac.com/readcsv.html
- type the machine tag
- The lisp gives me all other machines tag connected to this machine. This what I am asking for
- Insert the data as MText or a table I can handle using Entmakex.

I hope it is clear.
Attched CSV Cables Data
Title: Re: How to extract data from this list?
Post by: HasanCAD on March 23, 2019, 07:04:45 AM
Maybe rebuild the list is one option:
...

Thanks Marc'Antonio
But not showing the other tag
Title: Re: How to extract data from this list?
Post by: HasanCAD on March 23, 2019, 07:22:03 AM
Maybe something like this?
Thanks for your help but gives error
Quote
; error: bad argument type: consp "HVT-16-0120"

Edit: Working perfect There was mistake from my side.
Thanks kpblc
Title: Re: How to extract data from this list?
Post by: HasanCAD on March 23, 2019, 07:48:20 AM
Code - Auto/Visual Lisp: [Select]
  1. (defun f ( k aL )
  2.   (apply 'append (mapcar '(lambda (x) (if (member k (cdr x)) (list (vl-remove k x)))) aL))
  3. )

Thanks Grrr1337
Working perfect
Title: Re: How to extract data from this list?
Post by: HasanCAD on March 23, 2019, 07:50:50 AM
Another -
Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( x l )
  2.     (mapcar '(lambda ( i ) (vl-remove x i)) (vl-remove-if-not '(lambda ( i ) (member x i)) l))
  3. )
Working perfect
Thanks Lee
And Thanks for ReadCSV