TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: JohnSnow on June 04, 2019, 01:13:06 AM

Title: Need Some thoughts on how to revised this list
Post by: JohnSnow on June 04, 2019, 01:13:06 AM
Hi guys,

I have list that looks something like this:
(list
      (list "Country" "America") (list "State" "California") (list "City" "Los Angeles") (list "Town" "AAA")
      (list "Country" "America") (list "State" "Alaska") (list "City" "Anchorage") (list "Town" "CCC")
      (list "Country" "America") (list "State" "California") (list "City" "Adelanto") (list "Town" "AAA")
      (list "Country" "Australia") (list "State" "Vitoria") (list "City" "Melbourne") (list "Town" "XXX") 
      ...
)

I have been thinking how to turn it to something like the following but has no clue.
(list
     (list (list "Counrty" "America")
            (list
                 (list (list "State" "California")
                        (list
                              (list (list "City" "Los Angeles")
                                     (list "Town" (list "AAA" "BBB" ... ))
                              )
                             (list (list "City" "Adelanto")
                               (list "Town" (list "AAA" "HHH" ...))
                        )
                        ...
                 )
                 (list (list "State" "Alaska")
                        ....
                 )
          )
     )
     (list (list "Counrty" "Australia")
     ...
     )
     ...
)


In short, I wanna make it look a 'tree' type list. The above is just an example. I am looking for a function to make it work for all this kind of lists.
The reason why I wanna revise this list like this is because I can get, say the towns under "America"-"California"-"Los Angeles" or any other towns under other Country-State-City, or any cities under any Country-State etc by using 'assoc' with the 'tree' type list so I dont need to go through the original list everytime.
 
This final tree type list can look different as long as you think it works better to get the 'sub-list' easier.

Any thoughts?

Thank you.

               
 
 




Title: Re: Need Some thoughts on how to revised this list
Post by: kdub_nz on June 04, 2019, 01:24:57 AM

Welcome to the Swamp,

You will need to complete your post to get any sensible response ..
Title: Re: Need Some thoughts on how to revised this list
Post by: JohnSnow on June 04, 2019, 01:30:00 AM
Sorry I clicked 'post' by accident. It has been completed now.
Title: Re: Need Some thoughts on how to revised this list
Post by: ronjonp on June 04, 2019, 09:59:42 AM
Maybe something here will help: http://www.theswamp.org/index.php?topic=35191.msg404387#msg404387
Title: Re: Need Some thoughts on how to revised this list
Post by: ribarm on June 04, 2019, 10:46:25 AM
Not 100% sure, but it worked on your example... You just forgot to wrap each line of your list into list function...

Code - Auto/Visual Lisp: [Select]
  1. (defun buildassoclst (l / unique ll rl rll tmpx g ass)
  2.  
  3.   (defun unique (l)
  4.     (if l
  5.       (cons (car l) (unique (vl-remove (car l) l)))
  6.     )
  7.   )
  8.  
  9.   (setq ll (apply 'mapcar (cons 'list l)))
  10.   (setq ll (mapcar '(lambda (x) (unique x)) ll))
  11.   (setq ll (mapcar '(lambda (x) (vl-sort x '(lambda (a b) (< (cadr a) (cadr b))))) ll))
  12.   (setq rl (mapcar '(lambda (x) (reverse x)) l))
  13.   (setq rll (reverse ll))
  14.   (while (cadr rll)
  15.     (foreach x (car rll)
  16.       (setq tmpx (unique (vl-remove-if-not '(lambda (a) (equal x (car a))) rl)))
  17.       (while tmpx
  18.         (if (assoc (cadar tmpx) g)
  19.           (setq g
  20.                  (subst
  21.                    (list
  22.                      (cadar tmpx)
  23.                      (if (and (= (type (caaadr (assoc (cadar tmpx) g))) 'str)
  24.                               (not (vl-every '(lambda (x) (= (type x) 'str))
  25.                                              (apply 'append (cadr (assoc (cadar tmpx) g)))
  26.                                    )
  27.                               )
  28.                          )
  29.                        (vl-list* (cadr (assoc (cadar tmpx) g))
  30.                                  (if (assoc x g)
  31.                                    (progn (setq g (vl-remove (setq ass (assoc x g)) g)) (list ass))
  32.                                    (list x)
  33.                                  )
  34.                        )
  35.                        (append (cadr (assoc (cadar tmpx) g))
  36.                                (if (assoc x g)
  37.                                  (progn (setq g (vl-remove (setq ass (assoc x g)) g)) (list ass))
  38.                                  (list x)
  39.                                )
  40.                        )
  41.                      )
  42.                    )
  43.                    (assoc (cadar tmpx) g)
  44.                    g
  45.                  )
  46.           )
  47.           (if (assoc x g)
  48.             (setq g (subst (cons (cadar tmpx) (list (assoc x g))) (assoc x g) g))
  49.             (setq g (cons (cons (cadar tmpx) (list (list x))) g))
  50.           )
  51.         )
  52.         (setq tmpx (cdr tmpx))
  53.       )
  54.     )
  55.     (setq rll (cdr rll)
  56.           rl  (mapcar 'cdr rl)
  57.     )
  58.   )
  59.   g
  60. )
  61.  
  62. ;Wrong list
  63. ;|
  64. (setq l
  65.        (list
  66.          (list (list "Country" "America")
  67.                (list "State" "Nebraska")
  68.                (list "City" "Belgrade")
  69.                (list "Town" "KKK")
  70.          )
  71.          (list (list "Country" "America")
  72.                (list "State" "Nebraska")
  73.                (list "City" "Belgrade")
  74.                (list "Town" "LLL")
  75.          )
  76.          (list (list "Country" "America")
  77.                (list "State" "Missouri")
  78.                (list "City" "Belgrade")
  79.                (list "Town" "III")
  80.          )
  81.          (list (list "Country" "America")
  82.                (list "State" "Missouri")
  83.                (list "City" "Belgrade")
  84.                (list "Town" "JJJ")
  85.          )
  86.          (list (list "Country" "America")
  87.                (list "State" "Minnesota")
  88.                (list "City" "Belgrade")
  89.                (list "Town" "GGG")
  90.          )
  91.          (list (list "Country" "America")
  92.                (list "State" "Minnesota")
  93.                (list "City" "Belgrade")
  94.                (list "Town" "HHH")
  95.          )
  96.          (list (list "Country" "America")
  97.                (list "State" "Maine")
  98.                (list "City" "Belgrade")
  99.                (list "Town" "EEE")
  100.          )
  101.          (list (list "Country" "America")
  102.                (list "State" "Maine")
  103.                (list "City" "Belgrade")
  104.                (list "Town" "FFF")
  105.          )
  106.          (list (list "Country" "America")
  107.                (list "State" "Montana")
  108.                (list "City" "Belgrade")
  109.                (list "Town" "CCC")
  110.          )
  111.          (list (list "Country" "America")
  112.                (list "State" "Montana")
  113.                (list "City" "Belgrade")
  114.                (list "Town" "DDD")
  115.          )
  116.          (list (list "Country" "Serbia")
  117.                (list "State" "Central Serbia")
  118.                (list "City" "Belgrade")
  119.                (list "Town" "AAA")
  120.          )
  121.          (list (list "Country" "Serbia")
  122.                (list "State" "Central Serbia")
  123.                (list "City" "Belgrade")
  124.                (list "Town" "BBB")
  125.          )
  126.          (list (list "Country" "America")
  127.                (list "State" "California")
  128.                (list "City" "Los Angeles")
  129.                (list "Town" "AAA")
  130.          )
  131.          (list (list "Country" "America")
  132.                (list "State" "California")
  133.                (list "City" "Los Angeles")
  134.                (list "Town" "BBB")
  135.          )
  136.          (list (list "Country" "America")
  137.                (list "State" "California")
  138.                (list "City" "Los Angeles")
  139.                (list "Town" "CCC")
  140.          )
  141.          (list (list "Country" "America")
  142.                (list "State" "California")
  143.                (list "City" "Los Angeles")
  144.                (list "Town" "DDD")
  145.          )
  146.          (list (list "Country" "America")
  147.                (list "State" "Alaska")
  148.                (list "City" "Anchorage")
  149.                (list "Town" "CCC")
  150.          )
  151.          (list (list "Country" "America")
  152.                (list "State" "California")
  153.                (list "City" "Adelanto")
  154.                (list "Town" "AAA")
  155.          )
  156.          (list (list "Country" "Australia")
  157.                (list "State" "Vitoria")
  158.                (list "City" "Melbourne")
  159.                (list "Town" "XXX")
  160.          )
  161.        )
  162. )
  163. |;
  164.  
  165. ;Good list
  166. (setq l
  167.        (list
  168.          (list (list "Country" "America")
  169.                (list "State" "Nebraska-Am")
  170.                (list "City" "Belgrade-Am-Ne")
  171.                (list "Town" "KKK-Am-Ne-Be")
  172.          )
  173.          (list (list "Country" "America")
  174.                (list "State" "Nebraska-Am")
  175.                (list "City" "Belgrade-Am-Ne")
  176.                (list "Town" "LLL-Am-Ne-Be")
  177.          )
  178.          (list (list "Country" "America")
  179.                (list "State" "Missouri-Am")
  180.                (list "City" "Belgrade-Am-Mis")
  181.                (list "Town" "III-Am-Mi-Be")
  182.          )
  183.          (list (list "Country" "America")
  184.                (list "State" "Missouri-Am")
  185.                (list "City" "Belgrade-Am-Mis")
  186.                (list "Town" "JJJ-Am-Mis-Be")
  187.          )
  188.          (list (list "Country" "America")
  189.                (list "State" "Minnesota-Am")
  190.                (list "City" "Belgrade-Am-Min")
  191.                (list "Town" "GGG-Am-Min-Be")
  192.          )
  193.          (list (list "Country" "America")
  194.                (list "State" "Minnesota-Am")
  195.                (list "City" "Belgrade-Am-Min")
  196.                (list "Town" "HHH-Am-Min-Be")
  197.          )
  198.          (list (list "Country" "America")
  199.                (list "State" "Maine-Am")
  200.                (list "City" "Belgrade-Am-Ma")
  201.                (list "Town" "EEE-Am-Ma-Be")
  202.          )
  203.          (list (list "Country" "America")
  204.                (list "State" "Maine-Am")
  205.                (list "City" "Belgrade-Am-Ma")
  206.                (list "Town" "FFF-Am-Ma-Be")
  207.          )
  208.          (list (list "Country" "America")
  209.                (list "State" "Montana-Am")
  210.                (list "City" "Belgrade-Am-Mo")
  211.                (list "Town" "CCC-Am-Mo-Be")
  212.          )
  213.          (list (list "Country" "America")
  214.                (list "State" "Montana-Am")
  215.                (list "City" "Belgrade-Am-Mo")
  216.                (list "Town" "DDD-Am-Mo-Be")
  217.          )
  218.          (list (list "Country" "Serbia")
  219.                (list "State" "Central Serbia")
  220.                (list "City" "Belgrade")
  221.                (list "Town" "AAA")
  222.          )
  223.          (list (list "Country" "Serbia")
  224.                (list "State" "Central Serbia")
  225.                (list "City" "Belgrade")
  226.                (list "Town" "BBB")
  227.          )
  228.          (list (list "Country" "America")
  229.                (list "State" "California")
  230.                (list "City" "Los Angeles")
  231.                (list "Town" "AAA")
  232.          )
  233.          (list (list "Country" "America")
  234.                (list "State" "California")
  235.                (list "City" "Los Angeles")
  236.                (list "Town" "BBB")
  237.          )
  238.          (list (list "Country" "America")
  239.                (list "State" "California")
  240.                (list "City" "Los Angeles")
  241.                (list "Town" "CCC")
  242.          )
  243.          (list (list "Country" "America")
  244.                (list "State" "California")
  245.                (list "City" "Los Angeles")
  246.                (list "Town" "DDD")
  247.          )
  248.          (list (list "Country" "America")
  249.                (list "State" "Alaska")
  250.                (list "City" "Anchorage")
  251.                (list "Town" "CCC")
  252.          )
  253.          (list (list "Country" "America")
  254.                (list "State" "California")
  255.                (list "City" "Adelanto")
  256.                (list "Town" "AAA")
  257.          )
  258.          (list (list "Country" "Australia")
  259.                (list "State" "Vitoria")
  260.                (list "City" "Melbourne")
  261.                (list "Town" "XXX")
  262.          )
  263.        )
  264. )
  265.  
  266. (princ "\n")
  267. (prin1 (buildassoclst l))
  268.  

HTH., M.R.
Title: Re: Need Some thoughts on how to revised this list
Post by: VovKa on June 04, 2019, 06:47:55 PM
i think it's possible to get the appropriate result by just running groupbykey (https://www.theswamp.org/index.php?topic=53515.0) function recursively
Title: Re: Need Some thoughts on how to revised this list
Post by: BIGAL on June 04, 2019, 11:48:49 PM
Another suggestion is make a big list and sort the list you can sort multiple levels deep so you end up with a single list organised in country state city town. If you then want separate list groups it can be done by simply comparing the next item in the list to the previous, the list name can be created on the fly using the SET method. So end up with multiple lists.

As you know the items order I would not worry about the "country" "state" etc in the list ("America"  "California" "Los Angeles" "AAA")

Code: [Select]
(defun sort4(lst / )
(setq lst (vl-sort lst '(lambda (x y)
(cond
((= (nth 1 x)(nth 1 y))
(< (nth 0 x)(nth 0 y)))
((< (nth 1 x)(nth 1 y)))
((< (nth 2  x)(nth 2 y)))
((< (nth 3 x)(nth 3 y)))
)))
)
)
Title: Re: Need Some thoughts on how to revised this list
Post by: ribarm on June 05, 2019, 02:49:55 AM
I've updated my code with more info :
The problem I see is that different countries may have the same city and each with different towns - this is the case of Belgrade : there is my city in Europe and there are several in USA in different states... So I changed the code finally to do what should (there were lacks), but for cities and states, you should consider adding better relation string - look in good and wrong lists I have posted in revision... So each different place should have only one and unique name... This is the only way it should work correctly IMHO...

Regards, M.R.
Title: Re: Need Some thoughts on how to revised this list
Post by: JohnSnow on June 13, 2019, 07:09:39 PM
Thanks everyone. Sorry been super busy lately. I will have a look on your guys reference and scripts. Many thanks.:)