Author Topic: looping nested lists for dictionaries and opendcl  (Read 151 times)

0 Members and 1 Guest are viewing this topic.

Dan113

  • Mosquito
  • Posts: 5
looping nested lists for dictionaries and opendcl
« on: December 07, 2017, 02:51:31 am »
I have the below list, ive been scratching my head with no joy about how to do this.
I need to iterate through the list and create a treemap in opendcl to display the list structure visually. I can then go on to create a dictionary structure to save the data.

The following is just an example but each of the nested lists can have an unspecified number of lists and each with a different depth so i dont think a nested foreach is suitable/possible??

Can someone point me in the right direction....

Code: [Select]
(setq lst '("P1"
    ("Data" ("D1")("D2")("D3" ("D3-1" "D3-2" ("X"))))
    ("Stru" ("Ft1")("Ft2" ("Ft2-1")))    
    ("Rom-A" ("Flr-1" ("East" ("A" "B" "C"))("West" ("A" "B"))("South"("D" "E"))("North" ("A" "F" "G"))))
    ("Rom-B" ("Flr-1" ("East" ("A" "B" ("C" "T")))("West" ("A" "B"))("North" ("A" ("F") "G"))))
    ("Rom-C" ("Flr-1" ("East" ("A" "B" "C"))("South"("D" "E"))("North" ("A" "F" "G"))))
   )

Grrr1337

  • Bull Frog
  • Posts: 453
Re: looping nested lists for dictionaries and opendcl
« Reply #1 on: December 07, 2017, 05:04:13 am »
Maybe this thread will help.

Dan113

  • Mosquito
  • Posts: 5
Re: looping nested lists for dictionaries and opendcl
« Reply #2 on: December 07, 2017, 07:03:47 am »
I dont have an assoc list to use as im trying to map the entire tree.

This is the closest ive got so far but it only creates a root and everything after it as a child.

I cant seem to catch the individual lists

Code: [Select]
(defun C:test ( / map-tree)

  (defun map-tree (func tree /)
    (cond
      ((null tree) nil)
      ((vl-consp tree)
       (cons (map-tree func (car tree))
     (map-tree func (cdr tree))))
      (t (setq chld (func tree chld))))
  )

  (defun add-child (a b)
    (dcl-tree-addchild Test/Form2/TreeControl1 (list (list b a)))
  )

  (defun c:Test/Form2#OnInitialize (/)
    (setq lst '("P1"
("Data" ("D1")("D2")("D3" ("D3-1" "D3-2" ("X"))))
("Stru" ("Ft1")("Ft2" ("Ft2-1")))
("Rom-A" ("Flr-1" ("East" ("A" "B" "C"))("West" ("A" "B"))("South"("D" "E"))("North" ("A" "F" "G"))))
("Rom-B" ("Flr-1" ("East" ("A" "B" ("C" "T")))("West" ("A" "B"))("North" ("A" ("F") "G"))))
("Rom-C" ("Flr-1" ("East" ("A" "B" "C"))("South"("D" "E"))("North" ("A" "F" "G"))))
)
    )   
   
    (setq chld (dcl-Tree-AddParent Test/Form2/TreeControl1 '(("Test"))))
    (map-tree add-child lst)
  )

  (dcl-Form-Show Test/Form2)
)

Grrr1337

  • Bull Frog
  • Posts: 453
Re: looping nested lists for dictionaries and opendcl
« Reply #3 on: December 07, 2017, 09:16:52 am »
But what arguments (add-child) should take?
Like for "Data":
Code: [Select]
"Data" '(("D1")("D2")("D3" ("D3-1" "D3-2" ("X"))))or
Code: [Select]
(foreach x '(("D1")("D2")("D3" ("D3-1" "D3-2" ("X")))) (add-child "Data" x))or
Code: [Select]
"Data" "P1"so then for "D1" would be:
Code: [Select]
"D1" "Data"
Is the mapping order important for (dcl-tree-addchild) ?

I'm pretty sure your problem is solvable, but the guys who can do it might be not familiar with odcl's functions and vice versa.

MP

  • Seagull
  • Posts: 17015
  • brevity != aggression
Re: looping nested lists for dictionaries and opendcl
« Reply #4 on: December 07, 2017, 10:34:22 am »
How are you generating / harvesting the source data?
\|// Set goal. Experiment tirelessly until
|oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox | About

Dan113

  • Mosquito
  • Posts: 5
Re: looping nested lists for dictionaries and opendcl
« Reply #5 on: December 07, 2017, 03:07:16 pm »
The add-child arguments are the parent node and the name of the new child node.

I am obtaining the data from xrecords in dictionaries and I am wanting to display the contents to the user. The data originally comes from xdata attached to lines in a drawing and also user input. I am wanting to add to the xrecords data from the tree but this is only if I can get this to work.

VovKa

  • Swamp Rat
  • Posts: 873
  • Ukraine
Re: looping nested lists for dictionaries and opendcl
« Reply #6 on: December 07, 2017, 06:25:56 pm »
i use this
Code: [Select]
(defun vk_ODCL-FillTree (Control Lst)
  (mapcar (function
    (lambda (e / p)
      (cond ((listp (car e))
     (setq p (apply 'dcl_Tree_AddParent (cons Control (car e))))
     (cons p (vk_ODCL-FillTreeChildren Control p (cdr e)))
    )
    (t (apply 'dcl_Tree_AddParent (cons Control e)))
      )
    )
  )
  Lst
  )
)
(defun vk_ODCL-FillTreeChildren (Control Parent Children)
  (mapcar (function
    (lambda (c / p)
      (cond ((null c) (dcl_Tree_AddChild Control Parent ""))
    ((listp (car c))
     (setq p (apply 'dcl_Tree_AddChild
    (cons Control (cons Parent (car c)))
     )
     )
     (cons p (vk_ODCL-FillTreeChildren Control p (cdr c)))
    )
    (t
     (apply 'dcl_Tree_AddChild (cons Control (cons Parent c)))
    )
      )
    )
  )
  Children
  )
)
it will not work with the list you've provided
you'll have to either change the list structure or edit the functions