Author Topic: How to extract data from this list?  (Read 3718 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1421
How to extract data from this list?
« 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

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: How to extract data from this list?
« Reply #1 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 ??
« Last Edit: March 21, 2019, 06:46:58 AM by kdub »
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

Marc'Antonio Alessi

  • Swamp Rat
  • Posts: 1451
  • Marco
Re: How to extract data from this list?
« Reply #2 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")

kpblc

  • Bull Frog
  • Posts: 396
Re: How to extract data from this list?
« Reply #3 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"))
Sorry for my English.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: How to extract data from this list?
« Reply #4 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"))

(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: How to extract data from this list?
« Reply #5 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"))

HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: How to extract data from this list?
« Reply #6 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


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
« Last Edit: March 23, 2019, 07:06:14 AM by HasanCAD »

HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: How to extract data from this list?
« Reply #7 on: March 23, 2019, 07:04:45 AM »
Maybe rebuild the list is one option:
...

Thanks Marc'Antonio
But not showing the other tag

HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: How to extract data from this list?
« Reply #8 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
« Last Edit: March 23, 2019, 07:47:10 AM by HasanCAD »

HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: How to extract data from this list?
« Reply #9 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
« Last Edit: March 23, 2019, 08:04:44 AM by HasanCAD »

HasanCAD

  • Swamp Rat
  • Posts: 1421
Re: How to extract data from this list?
« Reply #10 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
« Last Edit: March 23, 2019, 08:05:55 AM by HasanCAD »