TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Marc'Antonio Alessi on December 07, 2017, 05:23:14 AM

Title: Puzzle: flat list to nested indexed list
Post by: Marc'Antonio Alessi on December 07, 2017, 05:23:14 AM


Maybe something similar has already been done, if someone remembers something like this...

how to get a nested indexed list from a flat list.
In_List  > length 1000
Out_List > 3 level each level is length 10
the contents of each line can be anything:
"SubFoo "      "Cl Foo"        "Foo "     "Description A"             "Foo B"           "F Measure mmm"
Code: [Select]

In_List '(
("SubGroup 0" "Class 00" "SubClass 000" "000 DescriptionA" "000 DescriptionB" "000 Measure")
("SubGroup 0" "Class 00" "SubClass 001" "001 DescriptionA" "001 DescriptionB" "001 Measure")
("SubGroup 0" "Class 00" "SubClass 002" "002 DescriptionA" "002 DescriptionB" "002 Measure")
...
("SubGroup 0" "Class 01" "SubClass 010" "010 DescriptionA" "010 DescriptionB" "010 Measure")
("SubGroup 0" "Class 01" "SubClass 011" "011 DescriptionA" "011 DescriptionB" "011 Measure")
("SubGroup 0" "Class 01" "SubClass 012" "012 DescriptionA" "012 DescriptionB" "012 Measure")
...
("SubGroup 0" "Class 02" "SubClass 020" "020 DescriptionA" "020 DescriptionB" "020 Measure")
("SubGroup 0" "Class 02" "SubClass 021" "021 DescriptionA" "021 DescriptionB" "021 Measure")
("SubGroup 0" "Class 02" "SubClass 022" "022 DescriptionA" "022 DescriptionB" "022 Measure")
...
("SubGroup 1" "Class 10" "SubClass 100" "100 DescriptionA" "100 DescriptionB" "100 Measure")
("SubGroup 1" "Class 10" "SubClass 101" "101 DescriptionA" "101 DescriptionB" "101 Measure")
("SubGroup 1" "Class 10" "SubClass 102" "102 DescriptionA" "102 DescriptionB" "102 Measure")
...
("SubGroup 1" "Class 11" "SubClass 110" "110 DescriptionA" "110 DescriptionB" "110 Measure")
("SubGroup 1" "Class 11" "SubClass 111" "111 DescriptionA" "111 DescriptionB" "111 Measure")
("SubGroup 1" "Class 11" "SubClass 112" "112 DescriptionA" "112 DescriptionB" "112 Measure")
...
("SubGroup 1" "Class 12" "SubClass 120" "120 DescriptionA" "120 DescriptionB" "120 Measure")
("SubGroup 1" "Class 12" "SubClass 121" "121 DescriptionA" "121 DescriptionB" "121 Measure")
("SubGroup 1" "Class 12" "SubClass 122" "122 DescriptionA" "122 DescriptionB" "122 Measure")
... last item is:
("SubGroup 9" "Class 99" "SubClass 999" "999 DescriptionA" "999 DescriptionB" "999 Measure")
)
Code: [Select]
Out_List '(
(0 "SubGroup 0"
  (0 "Class 00"
    (0 "SubClass 000" "000 DescriptionA" "000 DescriptionB" "000 Measure")
    (1 "SubClass 001" "001 DescriptionA" "001 DescriptionB" "001 Measure")
    (2 "SubClass 002" "002 DescriptionA" "002 DescriptionB" "002 Measure")
    ...
  )
  (1 "Class 01"
    (0 "SubClass 010" "010 DescriptionA" "010 DescriptionB" "010 Measure")
    (1 "SubClass 011" "011 DescriptionA" "011 DescriptionB" "011 Measure")
    (2 "SubClass 012" "012 DescriptionA" "012 DescriptionB" "012 Measure")
    ...
  )
  (2 "Class 02"
    (0 "SubClass 020" "020 DescriptionA" "020 DescriptionB" "020 Measure")
    (1 "SubClass 021" "021 DescriptionA" "021 DescriptionB" "021 Measure")
    (2 "SubClass 022" "022 DescriptionA" "022 DescriptionB" "022 Measure")
  )
  ...
)(1 "SubGroup 1"
  (0 "Class 10"
    (0 "SubClass 100" "100 DescriptionA" "100 DescriptionB" "100 Measure")
    (1 "SubClass 101" "101 DescriptionA" "101 DescriptionB" "101 Measure")
    (2 "SubClass 102" "102 DescriptionA" "102 DescriptionB" "102 Measure")
    ...
  )
  (1 "Class 11"
    (0 "SubClass 110" "110 DescriptionA" "110 DescriptionB" "110 Measure")
    (1 "SubClass 111" "111 DescriptionA" "111 DescriptionB" "111 Measure")
    (2 "SubClass 112" "112 DescriptionA" "112 DescriptionB" "112 Measure")
    ...
  )
  (2 "Class 12"
    (0 "SubClass 120" "120 DescriptionA" "120 DescriptionB" "120 Measure")
    (1 "SubClass 121" "121 DescriptionA" "121 DescriptionB" "121 Measure")
    (2 "SubClass 122" "122 DescriptionA" "122 DescriptionB" "122 Measure")
    ...
  )
  ...
)
... last item is:
(9 "SubGroup 9"
  (0 "Class 90"
    (0 "SubClass 900" "900 DescriptionA" "900 DescriptionB" "900 Measure")
    (1 "SubClass 901" "901 DescriptionA" "901 DescriptionB" "901 Measure")
    (2 "SubClass 902" "902 DescriptionA" "902 DescriptionB" "902 Measure")
    ...
  )
...
  (9 "Class 99"
    ... last item is:
    (9 "SubClass 999" "999 DescriptionA" "999 DescriptionB" "999 Measure")
  )
)
)
Code: [Select]
The goal is:
(defun Dxf (DxfCod EntDat)  (cdr (assoc DxfCod EntDat)))


To find the DescriptionB of SubClass 999:
(caddr (DXF 9 (cdr (DXF 9 (cdr (DXF 9 Out_List)))))))             => "999 DescriptionB"


(setq Pfx_SG 7   Pfx_CL 8   Pfx_SC 4)


To find the Description of SubGroup:
(DXF Pfx_SG Out_List)                                             => "SubGroup 7"


To find the Description of Class:
(car (DXF Pfx_CL (cdr (DXF Pfx_SG Out_List))))                    => "Class 78"


To find the Description of SubClass:
(car (DXF Pfx_SC (cdr (DXF Pfx_CL (cdr (DXF Pfx_SG Out_List)))))) => "SubClass 784"
Title: Re: Puzzle: flat list to nested indexed list
Post by: roy_043 on December 08, 2017, 05:29:51 AM
My approach:
Code - Auto/Visual Lisp: [Select]
  1. ; Divide a list into sublists with a fixed length. The last sublist in
  2. ; the return value can contain less elements than the other sublists.
  3. ; (KGA_List_Divide '(1 2 3 4 5 6 7 8) 3) => ((1 2 3) (4 5 6) (7 8))
  4. (defun KGA_List_Divide (lst n / i ret tmp)
  5.   (if lst
  6.     (progn
  7.       (setq i n)
  8.       (mapcar
  9.         '(lambda (a)
  10.           (if (zerop i)
  11.             (progn
  12.               (setq ret (cons (reverse tmp) ret))
  13.               (setq tmp nil)
  14.               (setq i n)
  15.             )
  16.           )
  17.           (setq tmp (cons a tmp))
  18.           (setq i (1- i))
  19.         )
  20.         lst
  21.       )
  22.       (reverse (cons (reverse tmp) ret))
  23.     )
  24.   )
  25. )
  26.  
  27. (defun DoAssoc (lst / fnd ret)
  28.   (foreach sub lst
  29.     (if (setq fnd (assoc (car sub) ret))
  30.       (setq ret (subst (append (list (car sub) (cdr sub)) (cdr fnd)) fnd ret))
  31.       (setq ret (cons (list (car sub) (cdr sub)) ret))
  32.     )
  33.   )
  34.     (mapcar
  35.       '(lambda (sub) (cons (car sub) (reverse (cdr sub))))
  36.       ret
  37.     )
  38.   )
  39. )
  40.  
  41. ; (Doit In_List)
  42. (defun DoIt (lst)
  43.   (mapcar
  44.     '(lambda (sub)
  45.       (cons (car sub) (DoAssoc (cdr sub)))
  46.     )
  47.     (DoAssoc (KGA_List_Divide lst 6))
  48.   )
  49. )
Title: Re: Puzzle: flat list to nested indexed list
Post by: Marc'Antonio Alessi on December 08, 2017, 07:25:11 AM

My approach:
...
Thanks Roy I will try it.  :)
Title: Re: Puzzle: flat list to nested indexed list
Post by: Lee Mac on December 08, 2017, 01:04:40 PM
You've actually already written a function to perform this operation in this thread (https://www.theswamp.org/index.php?topic=53515.0).  :wink:
Title: Re: Puzzle: flat list to nested indexed list
Post by: Marc'Antonio Alessi on December 08, 2017, 02:58:14 PM

You've actually already written a function to perform this operation in this thread (https://www.theswamp.org/index.php?topic=53515.0).  ;)
Code: [Select]
Thanks Lee but it's not the same thing:
>>> the contents of each line can be anything:
>>> "SubFoo "      "Cl Foo"        "Foo "     "Description A"             "Foo B"           "F Measure mmm"
there are no keys in the list, only the position in the list determines the key
now I'm trying to populate the list in a file so I can use it without recreating it every time
Code: [Select]
; sample for 3 level list each level is length 3 (NOT working)
(defun test (In_List / )
    (setq FilLsp "C:\\Temp\\Group6Info.lsp")           
      (setq FilPnt (open FilLsp "w")  Cur_SG 0  Cur_CL 0  Cur_SC 0)
      (write-line "(setq #Group6Info '(" FilPnt)             
      (foreach ForItm In_List
        (cond
          ( (= Cur_CL 0)
            (write-line "(" FilPnt) (prin1 Cur_SG FilPnt) (prin1 (car   ForItm) FilPnt)
            (write-line "(" FilPnt) (prin1 Cur_CL FilPnt) (prin1 (cadr  ForItm) FilPnt)
            (write-line "(" FilPnt) (prin1 Cur_SC FilPnt) (prin1 (caddr ForItm) FilPnt)
               (prin1 (nth 3 ForItm)) (prin1 (nth 4 ForItm))(prin1 (nth 5 ForItm)) (prin1 ")" FilPnt)
            (setq Cur_SC (1+ Cur_SC))
          )
          ( (and (= Cur_CL 2) (= Cur_SC 2))
            (write-line "(" FilPnt) (prin1 Cur_SC FilPnt) (prin1 (caddr ForItm) FilPnt) (prin1 (nth 3 ForItm))
            (prin1 (nth 4 ForItm))(prin1 (nth 5 ForItm)) (prin1 ")" FilPnt)
            (write-line ")" FilPnt)
            (write-line ")" FilPnt)
            (setq Cur_SG (1+ Cur_SG)  Cur_CL 0  Cur_SC 0)
          )
          ( T
                (write-line "(" FilPnt) (prin1 Cur_SC FilPnt) (prin1 (caddr ForItm) FilPnt) (prin1 (nth 3 ForItm))
                (prin1 (nth 4 ForItm))(prin1 (nth 5 ForItm)) (prin1 ")" FilPnt)
                (setq Cur_SC (1+ Cur_SC))
          )
        )
      )
      (write-line "))" FilPnt)
      (close FilPnt)
)
(setq In_List '(
("SubGroup 0" "Class 00" "SubClass 000" "000 DescriptionA" "000 DescriptionB" "000 Measure")
("SubGroup 0" "Class 00" "SubClass 001" "001 DescriptionA" "001 DescriptionB" "001 Measure")
("SubGroup 0" "Class 00" "SubClass 002" "002 DescriptionA" "002 DescriptionB" "002 Measure")
("SubGroup 0" "Class 01" "SubClass 010" "010 DescriptionA" "010 DescriptionB" "010 Measure")
("SubGroup 0" "Class 01" "SubClass 011" "011 DescriptionA" "011 DescriptionB" "011 Measure")
("SubGroup 0" "Class 01" "SubClass 012" "012 DescriptionA" "012 DescriptionB" "012 Measure")
("SubGroup 0" "Class 02" "SubClass 020" "020 DescriptionA" "020 DescriptionB" "020 Measure")
("SubGroup 0" "Class 02" "SubClass 021" "021 DescriptionA" "021 DescriptionB" "021 Measure")
("SubGroup 0" "Class 02" "SubClass 022" "022 DescriptionA" "022 DescriptionB" "022 Measure")
("SubGroup 1" "Class 10" "SubClass 100" "100 DescriptionA" "100 DescriptionB" "100 Measure")
("SubGroup 1" "Class 10" "SubClass 101" "101 DescriptionA" "101 DescriptionB" "101 Measure")
("SubGroup 1" "Class 10" "SubClass 102" "102 DescriptionA" "102 DescriptionB" "102 Measure")
("SubGroup 1" "Class 11" "SubClass 110" "110 DescriptionA" "110 DescriptionB" "110 Measure")
("SubGroup 1" "Class 11" "SubClass 111" "111 DescriptionA" "111 DescriptionB" "111 Measure")
("SubGroup 1" "Class 11" "SubClass 112" "112 DescriptionA" "112 DescriptionB" "112 Measure")
("SubGroup 1" "Class 12" "SubClass 120" "120 DescriptionA" "120 DescriptionB" "120 Measure")
("SubGroup 1" "Class 12" "SubClass 121" "121 DescriptionA" "121 DescriptionB" "121 Measure")
("SubGroup 1" "Class 12" "SubClass 122" "122 DescriptionA" "122 DescriptionB" "122 Measure")
("SubGroup 2" "Class 20" "SubClass 200" "200 DescriptionA" "200 DescriptionB" "200 Measure")
("SubGroup 2" "Class 20" "SubClass 201" "201 DescriptionA" "201 DescriptionB" "201 Measure")
("SubGroup 2" "Class 20" "SubClass 202" "202 DescriptionA" "202 DescriptionB" "202 Measure")
("SubGroup 2" "Class 21" "SubClass 210" "210 DescriptionA" "210 DescriptionB" "210 Measure")
("SubGroup 2" "Class 21" "SubClass 211" "211 DescriptionA" "211 DescriptionB" "211 Measure")
("SubGroup 2" "Class 21" "SubClass 212" "212 DescriptionA" "212 DescriptionB" "212 Measure")
("SubGroup 2" "Class 22" "SubClass 220" "220 DescriptionA" "220 DescriptionB" "220 Measure")
("SubGroup 2" "Class 22" "SubClass 221" "221 DescriptionA" "221 DescriptionB" "221 Measure")
("SubGroup 2" "Class 22" "SubClass 222" "222 DescriptionA" "222 DescriptionB" "222 Measure")
))

(test In_List)
Title: Re: Puzzle: flat list to nested indexed list
Post by: Grrr1337 on December 08, 2017, 03:30:24 PM
I think that the suggested codes in Split List Using Test Function (https://www.theswamp.org/index.php?topic=53354.msg580794#msg580794) thread might be handy, then use GroupByN on the list result, where n=2.
Title: Re: Puzzle: flat list to nested indexed list
Post by: Lee Mac on December 08, 2017, 04:15:37 PM
You've actually already written a function to perform this operation in this thread (https://www.theswamp.org/index.php?topic=53515.0).  ;)
Thanks Lee but it's not the same thing:
>>> the contents of each line can be anything:
>>> "SubFoo "      "Cl Foo"        "Foo "     "Description A"             "Foo B"           "F Measure mmm"
there are no keys in the list, only the position in the list determines the key

Using the first function I posted in that thread on the in_list list provided in your post above, the following should yield the desired result, no?
Code - Auto/Visual Lisp: [Select]
  1. (mapcar '(lambda ( x ) (cons (car x) (groupbykey (cdr x)))) (groupbykey in_list))
Title: Re: Puzzle: flat list to nested indexed list
Post by: Grrr1337 on December 08, 2017, 04:53:42 PM
Using the first function I posted in that thread on the in_list list provided in your post above, the following should yield the desired result, no?
Code - Auto/Visual Lisp: [Select]
  1. (mapcar '(lambda ( x ) (cons (car x) (groupbykey (cdr x)))) (groupbykey in_list))

Works, for the last in_list that Marco provided - which is list of lists of strings.
The very first (original) one in_list which is list of strings, should be processed a bit...
...with the subfunctions you wrote and that example you provided!  :-D

Initially defining...
Code - Auto/Visual Lisp: [Select]
  1. (defun SplitByTf ( prd lst / rtn tmp )
  2.   ; Lee Mac ; https://www.theswamp.org/index.php?topic=53354.msg580794#msg580794
  3.   (foreach itm lst
  4.     (if (apply prd (list itm))
  5.       (progn
  6.         (if tmp (setq rtn (cons (reverse tmp) rtn)))
  7.         (setq rtn (cons (list itm) rtn)
  8.           tmp nil
  9.         )
  10.       )
  11.       (setq tmp (cons itm tmp))
  12.     )
  13.   )
  14.   (reverse (if tmp (cons (reverse tmp) rtn) rtn))
  15. )
  16.  
  17. ; _$ (GroupBy2 '(1 2 3 4 5 6 7)) -> ((1 2) (3 4) (5 6) (7))
  18. (defun GroupBy2 ( L / tmp ) (cond ( (not L) L) ( (cons (append (if (setq tmp (car L)) (list tmp)) (if (setq tmp (cadr L)) (list tmp))) (GroupBy2 (cddr L))) ) ))
  19.  
  20. (defun groupbykey ( lst / rtn tmp )
  21.   ; Lee Mac ; https://www.theswamp.org/index.php?topic=53515.0
  22.   (foreach itm (reverse lst)
  23.     (if (setq tmp (assoc (car itm) rtn))
  24.       (setq rtn (subst (vl-list* (car itm) (cdr itm) (cdr tmp)) tmp rtn))
  25.       (setq rtn (cons  (list (car itm) (cdr itm)) rtn))
  26.     )
  27.   )
  28. )

and then:
Code: [Select]
(mapcar '(lambda ( x ) (cons (car x) (groupbykey (cdr x)))) 
  (groupbykey (mapcar '(lambda (x) (apply 'append x)) (GroupBy2 (SplitByTf '(lambda (x) (wcmatch x "SubGroup #*")) In_List))))
)
Title: Re: Puzzle: flat list to nested indexed list
Post by: Marc'Antonio Alessi on December 09, 2017, 06:26:41 AM

Works, for the last in_list that Marco provided - which is list of lists of strings.
The very first (original) one in_list which is list of strings, should be processed a bit...
...with the subfunctions you wrote and that example you provided!  ;D
Y
ou're right, I've corrected the first list that was missing parenthesis-
[/size][/color]
Thank you.
Title: Re: Puzzle: flat list to nested indexed list
Post by: ribarm on December 09, 2017, 09:36:20 AM
Just a thought, maybe something like this (foo lst) subroutine...

Code - Auto/Visual Lisp: [Select]
  1. (defun groupbykey ( lst / tmp rtn )
  2.   (foreach itm (reverse lst)
  3.     (if (setq tmp (assoc (car itm) rtn))
  4.       (setq rtn (subst (vl-list* (car itm) (cdr itm) (cdr tmp)) tmp rtn))
  5.       (setq rtn (cons (list (car itm) (cdr itm)) rtn))
  6.     )
  7.   )
  8.   rtn
  9. )
  10.  
  11. ;; (setq l '((0 1 2) (1 2 3) (2 3 4)))
  12. ;; (groupbykey l) => ((0 (1 2)) (1 (2 3)) (2 (3 4)))
  13.  
  14. ;; (setq l '((0 1 2 3) (0 1 2 3 4) (0 1 2 3 4 5)))
  15. ;; (groupbykey l) => ((0 (1 2 3) (1 2 3 4) (1 2 3 4 5)))
  16.  
  17. ;; (setq l '((0 1 2 3) (0 1 2 3 4) (0 1 2 3 4 5)))
  18. ;; (mapcar '(lambda ( x ) (cons (car x) (groupbykey (cdr x)))) (groupbykey l)) => ((0 (1 (2 3) (2 3 4) (2 3 4 5))))
  19.  
  20. (defun foo ( lst )
  21.   (if (and (setq lst (vl-remove 'nil lst)) (vl-every '(lambda ( x ) (assoc (caar lst) (list x))) lst))
  22.     (list (cons (caar lst) (foo (mapcar 'cdr lst))))
  23.   )
  24. )
  25.  
  26. ;; (mapcar '(lambda ( a ) (cons (car a) (groupbykey (cdr a)))) (cddr (mapcar '(lambda ( b ) (cons (car b) (groupbykey (cdr b)))) (cdr (mapcar '(lambda ( c ) (cons (car c) (groupbykey (cdr c)))) (groupbykey l)))))) => nil
  27. ;; (foo l) => ((0 (1 (2 (3 (4 (5)))))))
  28.  
Title: Re: Puzzle: flat list to nested indexed list
Post by: Marc'Antonio Alessi on December 09, 2017, 09:51:57 AM

Code: [Select]
This is a sample very rudimentary to get a nested indexed list from a flat list (and write to a file).
In_List  > length 27 - the contents of each line can be anything - there are not key in the list, the sample is only to test the results
Out_List > 3 level each level is length 3 >>>> In_List is with the same structure but "linear" or "flat"
Code: [Select]
(defun test (In_List / FilPnt)
    (setq FilLsp "C:\\Temp\\Group6Info.lsp")           
      (setq FilPnt (open FilLsp "w")  Cur_SG 0  Cur_CL 0  Cur_SC 0)
      (write-line "(setq #Group6Info '(" FilPnt)             
      (foreach ForItm In_List
        (cond
          ( (and (= Cur_CL 0) (= Cur_SC 0)) 
            (princ "\n(" FilPnt)  (princ Cur_SG FilPnt) (prin1 (car   ForItm) FilPnt)
            (princ "\n(" FilPnt)  (princ Cur_CL FilPnt) (prin1 (cadr  ForItm) FilPnt)
            (princ "\n(" FilPnt)  (princ Cur_SC FilPnt) (prin1 (caddr ForItm) FilPnt)
               (prin1 (nth 3 ForItm) FilPnt) (prin1 (nth 4 ForItm) FilPnt) (prin1 (nth 5 ForItm) FilPnt)
            (princ ")" FilPnt)
            (setq Cur_SC (1+ Cur_SC))
          )
          ( (= Cur_SC 0)
            (princ "\n(" FilPnt)  (princ Cur_CL FilPnt) (prin1 (cadr  ForItm) FilPnt)
            (princ "\n(" FilPnt)  (princ Cur_SC FilPnt) (prin1 (caddr ForItm) FilPnt)
               (prin1 (nth 3 ForItm) FilPnt) (prin1 (nth 4 ForItm) FilPnt) (prin1 (nth 5 ForItm) FilPnt)
            (princ ")" FilPnt)
            (setq Cur_SC (1+ Cur_SC))
          )
          ( (and (= Cur_CL 2) (= Cur_SC 2))
            (princ "\n(" FilPnt)  (princ Cur_SC FilPnt) (prin1 (caddr ForItm) FilPnt)
               (prin1 (nth 3 ForItm) FilPnt) (prin1 (nth 4 ForItm) FilPnt) (prin1 (nth 5 ForItm) FilPnt)
            (princ ")" FilPnt)
            (princ "\n))" FilPnt)
            (setq Cur_SG (1+ Cur_SG)  Cur_CL 0  Cur_SC 0)
          )
          ( (= Cur_SC 2)
            (princ "\n(" FilPnt) (prin1 Cur_SC FilPnt) (prin1 (caddr ForItm) FilPnt)
              (prin1 (nth 3 ForItm) FilPnt)  (prin1 (nth 4 ForItm) FilPnt)  (prin1 (nth 5 ForItm)  FilPnt)
            (princ "))" FilPnt)
            (setq Cur_CL (1+ Cur_CL)  Cur_SC 0)
          )
          ( T
               (princ "\n(" FilPnt) (prin1 Cur_SC FilPnt) (prin1 (caddr ForItm) FilPnt)
               (prin1 (nth 3 ForItm) FilPnt)  (prin1 (nth 4 ForItm) FilPnt)(prin1 (nth 5 ForItm) FilPnt) (princ ")" FilPnt)
               (setq Cur_SC (1+ Cur_SC))
          )
        )
      )
      (write-line "))" FilPnt)
      (close FilPnt)
)
(setq In_List '(
("SubGroup 0" "Class 00" "SubClass 000" "000 DescriptionA" "000 DescriptionB" "000 Measure")
("SubGroup 0" "Class 00" "SubClass 001" "001 DescriptionA" "001 DescriptionB" "001 Measure")
("SubGroup 0" "Class 00" "SubClass 002" "002 DescriptionA" "002 DescriptionB" "002 Measure")
("SubGroup 0" "Class 01" "SubClass 010" "010 DescriptionA" "010 DescriptionB" "010 Measure")
("SubGroup 0" "Class 01" "SubClass 011" "011 DescriptionA" "011 DescriptionB" "011 Measure")
("SubGroup 0" "Class 01" "SubClass 012" "012 DescriptionA" "012 DescriptionB" "012 Measure")
("SubGroup 0" "Class 02" "SubClass 020" "020 DescriptionA" "020 DescriptionB" "020 Measure")
("SubGroup 0" "Class 02" "SubClass 021" "021 DescriptionA" "021 DescriptionB" "021 Measure")
("SubGroup 0" "Class 02" "SubClass 022" "022 DescriptionA" "022 DescriptionB" "022 Measure")
("SubGroup 1" "Class 10" "SubClass 100" "100 DescriptionA" "100 DescriptionB" "100 Measure")
("SubGroup 1" "Class 10" "SubClass 101" "101 DescriptionA" "101 DescriptionB" "101 Measure")
("SubGroup 1" "Class 10" "SubClass 102" "102 DescriptionA" "102 DescriptionB" "102 Measure")
("SubGroup 1" "Class 11" "SubClass 110" "110 DescriptionA" "110 DescriptionB" "110 Measure")
("SubGroup 1" "Class 11" "SubClass 111" "111 DescriptionA" "111 DescriptionB" "111 Measure")
("SubGroup 1" "Class 11" "SubClass 112" "112 DescriptionA" "112 DescriptionB" "112 Measure")
("SubGroup 1" "Class 12" "SubClass 120" "120 DescriptionA" "120 DescriptionB" "120 Measure")
("SubGroup 1" "Class 12" "SubClass 121" "121 DescriptionA" "121 DescriptionB" "121 Measure")
("SubGroup 1" "Class 12" "SubClass 122" "122 DescriptionA" "122 DescriptionB" "122 Measure")
("SubGroup 2" "Class 20" "SubClass 200" "200 DescriptionA" "200 DescriptionB" "200 Measure")
("SubGroup 2" "Class 20" "SubClass 201" "201 DescriptionA" "201 DescriptionB" "201 Measure")
("SubGroup 2" "Class 20" "SubClass 202" "202 DescriptionA" "202 DescriptionB" "202 Measure")
("SubGroup 2" "Class 21" "SubClass 210" "210 DescriptionA" "210 DescriptionB" "210 Measure")
("SubGroup 2" "Class 21" "SubClass 211" "211 DescriptionA" "211 DescriptionB" "211 Measure")
("SubGroup 2" "Class 21" "SubClass 212" "212 DescriptionA" "212 DescriptionB" "212 Measure")
("SubGroup 2" "Class 22" "SubClass 220" "220 DescriptionA" "220 DescriptionB" "220 Measure")
("SubGroup 2" "Class 22" "SubClass 221" "221 DescriptionA" "221 DescriptionB" "221 Measure")
("SubGroup 2" "Class 22" "SubClass 222" "222 DescriptionA" "222 DescriptionB" "222 Measure")
))
(test In_List)
(setq #Group6Info '(
(0"SubGroup 0"
  (0"Class 00"
   (0"SubClass 000""000 DescriptionA""000 DescriptionB""000 Measure")
   (1"SubClass 001""001 DescriptionA""001 DescriptionB""001 Measure")
   (2"SubClass 002""002 DescriptionA""002 DescriptionB""002 Measure"))
  (1"Class 01"
   (0"SubClass 010""010 DescriptionA""010 DescriptionB""010 Measure")
   (1"SubClass 011""011 DescriptionA""011 DescriptionB""011 Measure")
   (2"SubClass 012""012 DescriptionA""012 DescriptionB""012 Measure"))
  (2"Class 02"
   (0"SubClass 020""020 DescriptionA""020 DescriptionB""020 Measure")
   (1"SubClass 021""021 DescriptionA""021 DescriptionB""021 Measure")
   (2"SubClass 022""022 DescriptionA""022 DescriptionB""022 Measure")
))
(1"SubGroup 1"
  (0"Class 10"
   (0"SubClass 100""100 DescriptionA""100 DescriptionB""100 Measure")
   (1"SubClass 101""101 DescriptionA""101 DescriptionB""101 Measure")
   (2"SubClass 102""102 DescriptionA""102 DescriptionB""102 Measure"))
  (1"Class 11"
   (0"SubClass 110""110 DescriptionA""110 DescriptionB""110 Measure")
   (1"SubClass 111""111 DescriptionA""111 DescriptionB""111 Measure")
   (2"SubClass 112""112 DescriptionA""112 DescriptionB""112 Measure"))
  (2"Class 12"
   (0"SubClass 120""120 DescriptionA""120 DescriptionB""120 Measure")
   (1"SubClass 121""121 DescriptionA""121 DescriptionB""121 Measure")
   (2"SubClass 122""122 DescriptionA""122 DescriptionB""122 Measure")
))
(2"SubGroup 2"
  (0"Class 20"
   (0"SubClass 200""200 DescriptionA""200 DescriptionB""200 Measure")
   (1"SubClass 201""201 DescriptionA""201 DescriptionB""201 Measure")
   (2"SubClass 202""202 DescriptionA""202 DescriptionB""202 Measure"))
  (1"Class 21"
   (0"SubClass 210""210 DescriptionA""210 DescriptionB""210 Measure")
   (1"SubClass 211""211 DescriptionA""211 DescriptionB""211 Measure")
   (2"SubClass 212""212 DescriptionA""212 DescriptionB""212 Measure"))
  (2"Class 22"
   (0"SubClass 220""220 DescriptionA""220 DescriptionB""220 Measure")
   (1"SubClass 221""221 DescriptionA""221 DescriptionB""221 Measure")
   (2"SubClass 222""222 DescriptionA""222 DescriptionB""222 Measure")
))))
Code: [Select]
Comando: (caddr (DXF 1 (cdr (DXF 1 (cdr (DXF 1 #Group6Info))))))
"111 DescriptionB"
Comando: (setq Pfx_SG 1   Pfx_CL 2   Pfx_SC 0)
0
Comando: (DXF Pfx_SG #Group6Info)
("SubGroup 1" (0 "Class 10" (0 "SubClass 100" "100 DescriptionA" "100 DescriptionB" "100 Measure") (1 "SubClass 101" "101 DescriptionA" "101 DescriptionB" "101 Measure") (2 "SubClass 102" "102 DescriptionA" "102 DescriptionB" "102 Measure")) (1 "Class 11" (0 "SubClass 110" "110 DescriptionA" "110 DescriptionB" "110 Measure") (1 "SubClass 111" "111 DescriptionA" "111 DescriptionB" "111 Measure") (2 "SubClass 112" "112 DescriptionA" "112 DescriptionB" "112 Measure")) (2 "Class 12" (0 "SubClass 120" "120 DescriptionA" "120 DescriptionB" "120 Measure") (1 "SubClass 121" "121 DescriptionA" "121 DescriptionB" "121 Measure") (2 "SubClass 122" "122 DescriptionA" "122 DescriptionB" "122 Measure")))

Comando: (car (DXF Pfx_CL (cdr (DXF Pfx_SG #Group6Info))))

"Class 12"
Comando: (car (DXF Pfx_SC (cdr (DXF Pfx_CL (cdr (DXF Pfx_SG #Group6Info))))))
"SubClass 120"
Title: Re: Puzzle: flat list to nested indexed list
Post by: Marc'Antonio Alessi on December 09, 2017, 10:16:03 AM
this is the final use
Title: Re: Puzzle: flat list to nested indexed list
Post by: Lee Mac on December 09, 2017, 10:26:08 AM
Nice one Marc!

Perhaps this (https://www.theswamp.org/index.php?topic=50237) can help with the implementation?
Or have you specifically opted for edit box tiles such that the user may edit the stored values?
Title: Re: Puzzle: flat list to nested indexed list
Post by: Marc'Antonio Alessi on December 09, 2017, 11:01:32 AM
Thanks Lee it was just the example I was looking for!  :smitten: 
Code: [Select]
> Or have you specifically opted for edit box tiles such that the user may edit the stored values?
Yes, this serves to capture the normalized descriptions with the possibility of completing them and bringing them
 back to the title-block and also placing the prefix of belonging. The information comes from an Excel file where I
 can see the informations even in a "3D" sheet (see my e-book and image)
[/code]
Title: Re: Puzzle: flat list to nested indexed list
Post by: Grrr1337 on December 09, 2017, 11:20:22 AM
Thanks for sharing the usage idea, Marc!
I always appreciate such thing.
Title: Re: Puzzle: flat list to nested indexed list
Post by: Marc'Antonio Alessi on December 09, 2017, 11:27:02 AM
Thanks for sharing the usage idea, Marc!
I always appreciate such thing.
Grazie a te.  :)
Title: Re: Puzzle: flat list to nested indexed list
Post by: ribarm on December 09, 2017, 12:09:08 PM
I've tried once again, but no avail (over 3 levels deep)...
Maybe Lee can solve it :

Code - Auto/Visual Lisp: [Select]
  1. (defun groupbykey ( lst / tmp rtn )
  2.   (foreach itm (reverse lst)
  3.     (if (setq tmp (assoc (car itm) rtn))
  4.       (setq rtn (subst (vl-list* (car itm) (cdr itm) (cdr tmp)) tmp rtn))
  5.       (setq rtn (cons (list (car itm) (cdr itm)) rtn))
  6.     )
  7.   )
  8.   rtn
  9. )
  10.  
  11. ;; (setq l '((0 1 2) (1 2 3) (2 3 4)))
  12. ;; (groupbykey l) => ((0 (1 2)) (1 (2 3)) (2 (3 4)))
  13.  
  14. ;; (setq l '((0 1 2 3) (0 1 2 3 4) (0 1 2 3 4 5)))
  15. ;; (groupbykey l) => ((0 (1 2 3) (1 2 3 4) (1 2 3 4 5)))
  16.  
  17. ;; (setq l '((0 1 2 3) (0 1 2 3 4) (0 1 2 3 4 5)))
  18. ;; (mapcar '(lambda ( x ) (cons (car x) (groupbykey (cdr x)))) (groupbykey l)) => ((0 (1 (2 3) (2 3 4) (2 3 4 5))))
  19.  
  20. (defun foo ( lst / groupbykey rtn )
  21.  
  22.   (defun groupbykey ( lst / tmp rtn )
  23.     (foreach itm (reverse lst)
  24.       (if (setq tmp (assoc (car itm) rtn))
  25.         (setq rtn (subst (vl-list* (car itm) (cdr itm) (cdr tmp)) tmp rtn))
  26.         (setq rtn (cons (list (car itm) (cdr itm)) rtn))
  27.       )
  28.     )
  29.     rtn
  30.   )
  31.  
  32.   (setq rtn lst)
  33.   (setq rtn (groupbykey rtn))
  34.   (if (not (equal (groupbykey (cdar rtn)) (groupbykey (groupbykey (cdar rtn)))))
  35.     (cons (car rtn) (foo (cdr rtn)))
  36.   )
  37.   rtn
  38. )
  39.  
  40. ;; (mapcar '(lambda ( a ) (cons (car a) (groupbykey (cdr a)))) (cddr (mapcar '(lambda ( b ) (cons (car b) (groupbykey (cdr b)))) (cdr (mapcar '(lambda ( c ) (cons (car c) (groupbykey (cdr c)))) (groupbykey l)))))) => nil
  41. ;; (foo l) => ((0 (1 2 3) (1 2 3 4) (1 2 3 4 5))) ; needed : ((0 (1 (2 (3 (4 (5)))))))
  42.  
  43. ;; testing list over 3 levels deep :
  44. (setq In_List '(
  45. ("SubGroup 0" "Class 00" "SubClass 000" "000 DescriptionA" "000 DescriptionB" "000 Measure")
  46. ("SubGroup 0" "Class 00" "SubClass 000" "001 DescriptionA" "001 DescriptionB" "001 Measure")
  47. ("SubGroup 0" "Class 00" "SubClass 000" "002 DescriptionA" "002 DescriptionB" "002 Measure")
  48. ("SubGroup 0" "Class 01" "SubClass 010" "010 DescriptionA" "010 DescriptionB" "010 Measure")
  49. ("SubGroup 0" "Class 01" "SubClass 010" "011 DescriptionA" "011 DescriptionB" "011 Measure")
  50. ("SubGroup 0" "Class 01" "SubClass 010" "012 DescriptionA" "012 DescriptionB" "012 Measure")
  51. ("SubGroup 0" "Class 02" "SubClass 020" "020 DescriptionA" "020 DescriptionB" "020 Measure")
  52. ("SubGroup 0" "Class 02" "SubClass 020" "021 DescriptionA" "021 DescriptionB" "021 Measure")
  53. ("SubGroup 0" "Class 02" "SubClass 020" "022 DescriptionA" "022 DescriptionB" "022 Measure")
  54. ("SubGroup 1" "Class 10" "SubClass 100" "100 DescriptionA" "100 DescriptionB" "100 Measure")
  55. ("SubGroup 1" "Class 10" "SubClass 100" "101 DescriptionA" "101 DescriptionB" "101 Measure")
  56. ("SubGroup 1" "Class 10" "SubClass 100" "102 DescriptionA" "102 DescriptionB" "102 Measure")
  57. ("SubGroup 1" "Class 11" "SubClass 110" "110 DescriptionA" "110 DescriptionB" "110 Measure")
  58. ("SubGroup 1" "Class 11" "SubClass 110" "111 DescriptionA" "111 DescriptionB" "111 Measure")
  59. ("SubGroup 1" "Class 11" "SubClass 110" "112 DescriptionA" "112 DescriptionB" "112 Measure")
  60. ("SubGroup 1" "Class 12" "SubClass 120" "120 DescriptionA" "120 DescriptionB" "120 Measure")
  61. ("SubGroup 1" "Class 12" "SubClass 120" "121 DescriptionA" "121 DescriptionB" "121 Measure")
  62. ("SubGroup 1" "Class 12" "SubClass 120" "122 DescriptionA" "122 DescriptionB" "122 Measure")
  63. ("SubGroup 2" "Class 20" "SubClass 200" "200 DescriptionA" "200 DescriptionB" "200 Measure")
  64. ("SubGroup 2" "Class 20" "SubClass 200" "201 DescriptionA" "201 DescriptionB" "201 Measure")
  65. ("SubGroup 2" "Class 20" "SubClass 200" "202 DescriptionA" "202 DescriptionB" "202 Measure")
  66. ("SubGroup 2" "Class 21" "SubClass 210" "210 DescriptionA" "210 DescriptionB" "210 Measure")
  67. ("SubGroup 2" "Class 21" "SubClass 210" "211 DescriptionA" "211 DescriptionB" "211 Measure")
  68. ("SubGroup 2" "Class 21" "SubClass 210" "212 DescriptionA" "212 DescriptionB" "212 Measure")
  69. ("SubGroup 2" "Class 22" "SubClass 220" "220 DescriptionA" "220 DescriptionB" "220 Measure")
  70. ("SubGroup 2" "Class 22" "SubClass 220" "221 DescriptionA" "221 DescriptionB" "221 Measure")
  71. ("SubGroup 2" "Class 22" "SubClass 220" "222 DescriptionA" "222 DescriptionB" "222 Measure")
  72. ))
  73.  

 :-(
Title: Re: Puzzle: flat list to nested indexed list
Post by: Marc'Antonio Alessi on December 09, 2017, 02:37:44 PM
I've tried once again, but no avail (over 3 levels deep)...
Maybe Lee can solve it :
...
 :(
Code: [Select]
Marko, please  read carefully:
In_List  > length 1000
         - the contents of each element in the line can be anything also empty data
         - there are not keys in the list, the sample is only to test the results
         
Out_List > 10 level each level is length 10
         >>>> In_List is with the same structure but "linear" or "flat"   
       
Code: [Select]
This is valid for 10 level but is very rude...
Code: [Select]
     (foreach ForItm In_List
        (cond
          ( (and (= Cur_CL 0) (= Cur_SC 0)) 
            (princ "\n(" FilPnt)  (princ Cur_SG FilPnt) (princ " " FilPnt) (prin1 (car   ForItm) FilPnt)
            (princ "\n(" FilPnt)  (princ Cur_CL FilPnt) (princ " " FilPnt) (prin1 (cadr  ForItm) FilPnt)
            (princ "\n(" FilPnt)  (princ Cur_SC FilPnt) (princ " " FilPnt) (prin1 (caddr ForItm) FilPnt)
              (princ " " FilPnt) (prin1 (nth 3 ForItm) FilPnt) (princ " " FilPnt) (prin1 (nth 4 ForItm) FilPnt) (princ " " FilPnt)(prin1 (nth 5 ForItm) FilPnt)
            (princ ")" FilPnt)
            (setq Cur_SC (1+ Cur_SC))
          )
          ( (= Cur_SC 0)
            (princ "\n(" FilPnt)  (princ Cur_CL FilPnt) (princ " " FilPnt) (prin1 (cadr  ForItm) FilPnt)
            (princ "\n(" FilPnt)  (princ Cur_SC FilPnt) (princ " " FilPnt) (prin1 (caddr ForItm) FilPnt)
               (princ " " FilPnt) (prin1 (nth 3 ForItm) FilPnt) (princ " " FilPnt) (prin1 (nth 4 ForItm) FilPnt) (princ " " FilPnt) (prin1 (nth 5 ForItm) FilPnt)
            (princ ")" FilPnt)
            (setq Cur_SC (1+ Cur_SC))
          )
          ( (and (= Cur_CL 9) (= Cur_SC 9))
            (princ "\n(" FilPnt)  (princ Cur_SC FilPnt) (princ " " FilPnt) (prin1 (caddr ForItm) FilPnt)
               (princ " " FilPnt) (prin1 (nth 3 ForItm) FilPnt) (princ " " FilPnt) (prin1 (nth 4 ForItm) FilPnt) (princ " " FilPnt) (prin1 (nth 5 ForItm) FilPnt)
            (princ ")" FilPnt)
            (princ "\n))" FilPnt)
            (setq Cur_SG (1+ Cur_SG)  Cur_CL 0  Cur_SC 0)
          )
          ( (= Cur_SC 9)
            (princ "\n(" FilPnt) (prin1 Cur_SC FilPnt) (princ " " FilPnt) (prin1 (caddr ForItm) FilPnt)
              (princ " " FilPnt) (prin1 (nth 3 ForItm) FilPnt) (princ " " FilPnt)  (prin1 (nth 4 ForItm) FilPnt)  (princ " " FilPnt) (prin1 (nth 5 ForItm)  FilPnt)
            (princ "))" FilPnt)
            (setq Cur_CL (1+ Cur_CL)  Cur_SC 0)
          )
          ( T
            (princ "\n(" FilPnt) (prin1 Cur_SC FilPnt) (princ " " FilPnt) (prin1 (caddr ForItm) FilPnt)
            (prin1 (nth 3 ForItm) FilPnt)  (princ " " FilPnt) (prin1 (nth 4 ForItm) FilPnt) (princ " " FilPnt) (prin1 (nth 5 ForItm) FilPnt) (princ ")" FilPnt)
            (setq Cur_SC (1+ Cur_SC))
          )
        )
      )
Title: Re: Puzzle: flat list to nested indexed list
Post by: ribarm on December 09, 2017, 07:27:22 PM
This is as far as I went ab this task...

Code - Auto/Visual Lisp: [Select]
  1. (defun groupbykey ( lst / tmp rtn )
  2.   (foreach itm (reverse lst)
  3.     (if (setq tmp (assoc (car itm) rtn))
  4.       (setq rtn (subst (vl-list* (car itm) (cdr itm) (cdr tmp)) tmp rtn))
  5.       (setq rtn (cons (list (car itm) (cdr itm)) rtn))
  6.     )
  7.   )
  8.   rtn
  9. )
  10.  
  11. ;; (setq l '((0 1 2) (1 2 3) (2 3 4)))
  12. ;; (groupbykey l) => ((0 (1 2)) (1 (2 3)) (2 (3 4)))
  13.  
  14. ;; (setq l '((0 1 2 3) (0 1 2 3 4) (0 1 2 3 4 5)))
  15. ;; (groupbykey l) => ((0 (1 2 3) (1 2 3 4) (1 2 3 4 5)))
  16.  
  17. ;; (setq l '((0 1 2 3) (0 1 2 3 4) (0 1 2 3 4 5)))
  18. ;; (mapcar '(lambda ( x ) (cons (car x) (groupbykey (cdr x)))) (groupbykey l)) => ((0 (1 (2 3) (2 3 4) (2 3 4 5))))
  19.  
  20. (defun foo ( lst / groupbykey )
  21.  
  22.   (defun groupbykey ( lst / tmp rtn )
  23.     (foreach itm (reverse lst)
  24.       (if (setq tmp (assoc (car itm) rtn))
  25.         (setq rtn (subst (vl-list* (car itm) (cdr itm) (cdr tmp)) tmp rtn))
  26.         (setq rtn (cons (list (car itm) (cdr itm)) rtn))
  27.       )
  28.     )
  29.     rtn
  30.   )
  31.  
  32.   (if (car lst)
  33.     (mapcar '(lambda ( x ) (cons (car x) (foo (cdr x)))) (groupbykey lst))
  34.   )
  35. )
  36.  
  37. ;; (setq l '((0 1 2 3) (0 1 2 3 4) (0 1 2 3 4 5)))
  38. ;; (mapcar '(lambda ( a ) (cons (car a) (groupbykey (cdr a)))) (cddr (mapcar '(lambda ( b ) (cons (car b) (groupbykey (cdr b)))) (cdr (mapcar '(lambda ( c ) (cons (car c) (groupbykey (cdr c)))) (groupbykey l)))))) => nil
  39.  
  40. ;; (setq l '((0)))
  41. ;; (foo l) => ((0))
  42.  
  43. ;; (setq l '((0 1 2) (1 2 3) (2 3 4)))
  44. ;; (foo l) => ((0 (1 (2))) (1 (2 (3))) (2 (3 (4))))
  45.  
  46. ;; (setq l '((0 1 2 3) (0 1 2 3 4) (0 1 2 3 4 5)))
  47. ;; (foo l) => ((0 (1 (2 (3)))))
  48.  
  49. (defun fooo ( lst / groupbykey )
  50.  
  51.   (defun groupbykey ( lst / tmp rtn )
  52.     (foreach itm (reverse lst)
  53.       (if (setq tmp (assoc (car itm) rtn))
  54.         (setq rtn (subst (vl-list* (car itm) (cdr itm) (cdr tmp)) tmp rtn))
  55.         (setq rtn (cons (list (car itm) (cdr itm)) rtn))
  56.       )
  57.     )
  58.     rtn
  59.   )
  60.  
  61.   (if (/= (length lst) (length (groupbykey lst)))
  62.     (mapcar '(lambda ( x ) (cons (car x) (fooo (cdr x)))) (groupbykey lst))
  63.     lst
  64.   )
  65. )
  66.  
  67. ;; (setq l '((0)))
  68. ;; (fooo l) => ((0))
  69.  
  70. ;; (setq l '((0 1 2) (1 2 3) (2 3 4)))
  71. ;; (fooo l) => ((0 1 2) (1 2 3) (2 3 4))
  72.  
  73. ;; (setq l '((0 1 2 3) (0 1 2 3 4) (0 1 2 3 4 5)))
  74. ;; (fooo l) => ((0 (1 (2 (3 (nil nil) (4 nil (5)))))))
  75.  
  76. ;; testing list over 3 levels deep :
  77. (setq In_List '(
  78. ("SubGroup 0" "Class 00" "SubClass 000" "000 DescriptionA" "000 DescriptionB" "000 Measure")
  79. ("SubGroup 0" "Class 00" "SubClass 000" "001 DescriptionA" "001 DescriptionB" "001 Measure")
  80. ("SubGroup 0" "Class 00" "SubClass 000" "002 DescriptionA" "002 DescriptionB" "002 Measure")
  81. ("SubGroup 0" "Class 01" "SubClass 010" "010 DescriptionA" "010 DescriptionB" "010 Measure")
  82. ("SubGroup 0" "Class 01" "SubClass 010" "011 DescriptionA" "011 DescriptionB" "011 Measure")
  83. ("SubGroup 0" "Class 01" "SubClass 010" "012 DescriptionA" "012 DescriptionB" "012 Measure")
  84. ("SubGroup 0" "Class 02" "SubClass 020" "020 DescriptionA" "020 DescriptionB" "020 Measure")
  85. ("SubGroup 0" "Class 02" "SubClass 020" "021 DescriptionA" "021 DescriptionB" "021 Measure")
  86. ("SubGroup 0" "Class 02" "SubClass 020" "022 DescriptionA" "022 DescriptionB" "022 Measure")
  87. ("SubGroup 1" "Class 10" "SubClass 100" "100 DescriptionA" "100 DescriptionB" "100 Measure")
  88. ("SubGroup 1" "Class 10" "SubClass 100" "101 DescriptionA" "101 DescriptionB" "101 Measure")
  89. ("SubGroup 1" "Class 10" "SubClass 100" "102 DescriptionA" "102 DescriptionB" "102 Measure")
  90. ("SubGroup 1" "Class 11" "SubClass 110" "110 DescriptionA" "110 DescriptionB" "110 Measure")
  91. ("SubGroup 1" "Class 11" "SubClass 110" "111 DescriptionA" "111 DescriptionB" "111 Measure")
  92. ("SubGroup 1" "Class 11" "SubClass 110" "112 DescriptionA" "112 DescriptionB" "112 Measure")
  93. ("SubGroup 1" "Class 12" "SubClass 120" "120 DescriptionA" "120 DescriptionB" "120 Measure")
  94. ("SubGroup 1" "Class 12" "SubClass 120" "121 DescriptionA" "121 DescriptionB" "121 Measure")
  95. ("SubGroup 1" "Class 12" "SubClass 120" "122 DescriptionA" "122 DescriptionB" "122 Measure")
  96. ("SubGroup 2" "Class 20" "SubClass 200" "200 DescriptionA" "200 DescriptionB" "200 Measure")
  97. ("SubGroup 2" "Class 20" "SubClass 200" "201 DescriptionA" "201 DescriptionB" "201 Measure")
  98. ("SubGroup 2" "Class 20" "SubClass 200" "202 DescriptionA" "202 DescriptionB" "202 Measure")
  99. ("SubGroup 2" "Class 21" "SubClass 210" "210 DescriptionA" "210 DescriptionB" "210 Measure")
  100. ("SubGroup 2" "Class 21" "SubClass 210" "211 DescriptionA" "211 DescriptionB" "211 Measure")
  101. ("SubGroup 2" "Class 21" "SubClass 210" "212 DescriptionA" "212 DescriptionB" "212 Measure")
  102. ("SubGroup 2" "Class 22" "SubClass 220" "220 DescriptionA" "220 DescriptionB" "220 Measure")
  103. ("SubGroup 2" "Class 22" "SubClass 220" "221 DescriptionA" "221 DescriptionB" "221 Measure")
  104. ("SubGroup 2" "Class 22" "SubClass 220" "222 DescriptionA" "222 DescriptionB" "222 Measure")
  105. ))
  106.  
  107. ;; (foo In_List) => Out_List
  108. ;| Out_List
  109. (("SubGroup 0" ("Class 00" ("SubClass 000" ("000 DescriptionA" ("000 DescriptionB" ("000 Measure"))) ("001 DescriptionA" ("001 DescriptionB" ("001 Measure"))) ("002 DescriptionA" ("002 DescriptionB" ("002 Measure"))))) ("Class 01" ("SubClass 010" ("010 DescriptionA" ("010 DescriptionB" ("010 Measure"))) ("011 DescriptionA" ("011 DescriptionB" ("011 Measure"))) ("012 DescriptionA" ("012 DescriptionB" ("012 Measure"))))) ("Class 02" ("SubClass 020" ("020 DescriptionA" ("020 DescriptionB" ("020 Measure"))) ("021 DescriptionA" ("021 DescriptionB" ("021 Measure"))) ("022 DescriptionA" ("022 DescriptionB" ("022 Measure")))))) ("SubGroup 1" ("Class 10" ("SubClass 100" ("100 DescriptionA" ("100 DescriptionB" ("100 Measure"))) ("101 DescriptionA" ("101 DescriptionB" ("101 Measure"))) ("102 DescriptionA" ("102 DescriptionB" ("102 Measure"))))) ("Class 11" ("SubClass 110" ("110 DescriptionA" ("110 DescriptionB" ("110 Measure"))) ("111 DescriptionA" ("111 DescriptionB" ("111 Measure"))) ("112 DescriptionA" ("112 DescriptionB" ("112 Measure"))))) ("Class 12" ("SubClass 120" ("120 DescriptionA" ("120 DescriptionB" ("120 Measure"))) ("121 DescriptionA" ("121 DescriptionB" ("121 Measure"))) ("122 DescriptionA" ("122 DescriptionB" ("122 Measure")))))) ("SubGroup 2" ("Class 20" ("SubClass 200" ("200 DescriptionA" ("200 DescriptionB" ("200 Measure"))) ("201 DescriptionA" ("201 DescriptionB" ("201 Measure"))) ("202 DescriptionA" ("202 DescriptionB" ("202 Measure"))))) ("Class 21" ("SubClass 210" ("210 DescriptionA" ("210 DescriptionB" ("210 Measure"))) ("211 DescriptionA" ("211 DescriptionB" ("211 Measure"))) ("212 DescriptionA" ("212 DescriptionB" ("212 Measure"))))) ("Class 22" ("SubClass 220" ("220 DescriptionA" ("220 DescriptionB" ("220 Measure"))) ("221 DescriptionA" ("221 DescriptionB" ("221 Measure"))) ("222 DescriptionA" ("222 DescriptionB" ("222 Measure")))))))
  110. ( ("SubGroup 0"
  111.     ("Class 00"
  112.       ("SubClass 000"
  113.         ("000 DescriptionA" ("000 DescriptionB" ("000 Measure")))
  114.         ("001 DescriptionA" ("001 DescriptionB" ("001 Measure")))
  115.         ("002 DescriptionA" ("002 DescriptionB" ("002 Measure")))
  116.       )
  117.     )
  118.     ("Class 01"
  119.       ("SubClass 010"
  120.         ("010 DescriptionA" ("010 DescriptionB" ("010 Measure")))
  121.         ("011 DescriptionA" ("011 DescriptionB" ("011 Measure")))
  122.         ("012 DescriptionA" ("012 DescriptionB" ("012 Measure")))
  123.       )
  124.     )
  125.     ("Class 02"
  126.       ("SubClass 020"
  127.         ("020 DescriptionA" ("020 DescriptionB" ("020 Measure")))
  128.         ("021 DescriptionA" ("021 DescriptionB" ("021 Measure")))
  129.         ("022 DescriptionA" ("022 DescriptionB" ("022 Measure")))
  130.       )
  131.     )
  132.   )
  133.   ("SubGroup 1"
  134.     ("Class 10"
  135.       ("SubClass 100"
  136.         ("100 DescriptionA" ("100 DescriptionB" ("100 Measure")))
  137.         ("101 DescriptionA" ("101 DescriptionB" ("101 Measure")))
  138.         ("102 DescriptionA" ("102 DescriptionB" ("102 Measure")))
  139.       )
  140.     )
  141.     ("Class 11"
  142.       ("SubClass 110"
  143.         ("110 DescriptionA" ("110 DescriptionB" ("110 Measure")))
  144.         ("111 DescriptionA" ("111 DescriptionB" ("111 Measure")))
  145.         ("112 DescriptionA" ("112 DescriptionB" ("112 Measure")))
  146.       )
  147.     )
  148.     ("Class 12"
  149.       ("SubClass 120"
  150.         ("120 DescriptionA" ("120 DescriptionB" ("120 Measure")))
  151.         ("121 DescriptionA" ("121 DescriptionB" ("121 Measure")))
  152.         ("122 DescriptionA" ("122 DescriptionB" ("122 Measure")))
  153.       )
  154.     )
  155.   )
  156.   ("SubGroup 2"
  157.     ("Class 20"
  158.       ("SubClass 200"
  159.         ("200 DescriptionA" ("200 DescriptionB" ("200 Measure")))
  160.         ("201 DescriptionA" ("201 DescriptionB" ("201 Measure")))
  161.         ("202 DescriptionA" ("202 DescriptionB" ("202 Measure")))
  162.       )
  163.     )
  164.     ("Class 21"
  165.       ("SubClass 210"
  166.         ("210 DescriptionA" ("210 DescriptionB" ("210 Measure")))
  167.         ("211 DescriptionA" ("211 DescriptionB" ("211 Measure")))
  168.         ("212 DescriptionA" ("212 DescriptionB" ("212 Measure")))
  169.       )
  170.     )
  171.     ("Class 22"
  172.       ("SubClass 220"
  173.         ("220 DescriptionA" ("220 DescriptionB" ("220 Measure")))
  174.         ("221 DescriptionA" ("221 DescriptionB" ("221 Measure")))
  175.         ("222 DescriptionA" ("222 DescriptionB" ("222 Measure")))
  176.       )
  177.     )
  178.   )
  179. )
  180. |;
  181.  
  182. ;; (fooo In_List) => Out_List
  183. ;| Out_List
  184. (("SubGroup 0" ("Class 00" ("SubClass 000" ("000 DescriptionA" "000 DescriptionB" "000 Measure") ("001 DescriptionA" "001 DescriptionB" "001 Measure") ("002 DescriptionA" "002 DescriptionB" "002 Measure"))) ("Class 01" ("SubClass 010" ("010 DescriptionA" "010 DescriptionB" "010 Measure") ("011 DescriptionA" "011 DescriptionB" "011 Measure") ("012 DescriptionA" "012 DescriptionB" "012 Measure"))) ("Class 02" ("SubClass 020" ("020 DescriptionA" "020 DescriptionB" "020 Measure") ("021 DescriptionA" "021 DescriptionB" "021 Measure") ("022 DescriptionA" "022 DescriptionB" "022 Measure")))) ("SubGroup 1" ("Class 10" ("SubClass 100" ("100 DescriptionA" "100 DescriptionB" "100 Measure") ("101 DescriptionA" "101 DescriptionB" "101 Measure") ("102 DescriptionA" "102 DescriptionB" "102 Measure"))) ("Class 11" ("SubClass 110" ("110 DescriptionA" "110 DescriptionB" "110 Measure") ("111 DescriptionA" "111 DescriptionB" "111 Measure") ("112 DescriptionA" "112 DescriptionB" "112 Measure"))) ("Class 12" ("SubClass 120" ("120 DescriptionA" "120 DescriptionB" "120 Measure") ("121 DescriptionA" "121 DescriptionB" "121 Measure") ("122 DescriptionA" "122 DescriptionB" "122 Measure")))) ("SubGroup 2" ("Class 20" ("SubClass 200" ("200 DescriptionA" "200 DescriptionB" "200 Measure") ("201 DescriptionA" "201 DescriptionB" "201 Measure") ("202 DescriptionA" "202 DescriptionB" "202 Measure"))) ("Class 21" ("SubClass 210" ("210 DescriptionA" "210 DescriptionB" "210 Measure") ("211 DescriptionA" "211 DescriptionB" "211 Measure") ("212 DescriptionA" "212 DescriptionB" "212 Measure"))) ("Class 22" ("SubClass 220" ("220 DescriptionA" "220 DescriptionB" "220 Measure") ("221 DescriptionA" "221 DescriptionB" "221 Measure") ("222 DescriptionA" "222 DescriptionB" "222 Measure")))))
  185. ( ("SubGroup 0"
  186.     ("Class 00"
  187.       ("SubClass 000"
  188.         ("000 DescriptionA" "000 DescriptionB" "000 Measure")
  189.         ("001 DescriptionA" "001 DescriptionB" "001 Measure")
  190.         ("002 DescriptionA" "002 DescriptionB" "002 Measure")
  191.       )
  192.     )
  193.     ("Class 01"
  194.       ("SubClass 010"
  195.         ("010 DescriptionA" "010 DescriptionB" "010 Measure")
  196.         ("011 DescriptionA" "011 DescriptionB" "011 Measure")
  197.         ("012 DescriptionA" "012 DescriptionB" "012 Measure")
  198.       )
  199.     )
  200.     ("Class 02"
  201.       ("SubClass 020"
  202.         ("020 DescriptionA" "020 DescriptionB" "020 Measure")
  203.         ("021 DescriptionA" "021 DescriptionB" "021 Measure")
  204.         ("022 DescriptionA" "022 DescriptionB" "022 Measure")
  205.       )
  206.     )
  207.   )
  208.   ("SubGroup 1"
  209.     ("Class 10"
  210.       ("SubClass 100"
  211.         ("100 DescriptionA" "100 DescriptionB" "100 Measure")
  212.         ("101 DescriptionA" "101 DescriptionB" "101 Measure")
  213.         ("102 DescriptionA" "102 DescriptionB" "102 Measure")
  214.       )
  215.     )
  216.     ("Class 11"
  217.       ("SubClass 110"
  218.         ("110 DescriptionA" "110 DescriptionB" "110 Measure")
  219.         ("111 DescriptionA" "111 DescriptionB" "111 Measure")
  220.         ("112 DescriptionA" "112 DescriptionB" "112 Measure")
  221.       )
  222.     )
  223.     ("Class 12"
  224.       ("SubClass 120"
  225.         ("120 DescriptionA" "120 DescriptionB" "120 Measure")
  226.         ("121 DescriptionA" "121 DescriptionB" "121 Measure")
  227.         ("122 DescriptionA" "122 DescriptionB" "122 Measure")
  228.       )
  229.     )
  230.   )
  231.   ("SubGroup 2"
  232.     ("Class 20"
  233.       ("SubClass 200"
  234.         ("200 DescriptionA" "200 DescriptionB" "200 Measure")
  235.         ("201 DescriptionA" "201 DescriptionB" "201 Measure")
  236.         ("202 DescriptionA" "202 DescriptionB" "202 Measure")
  237.       )
  238.     )
  239.     ("Class 21"
  240.       ("SubClass 210"
  241.         ("210 DescriptionA" "210 DescriptionB" "210 Measure")
  242.         ("211 DescriptionA" "211 DescriptionB" "211 Measure")
  243.         ("212 DescriptionA" "212 DescriptionB" "212 Measure")
  244.       )
  245.     )
  246.     ("Class 22"
  247.       ("SubClass 220"
  248.         ("220 DescriptionA" "220 DescriptionB" "220 Measure")
  249.         ("221 DescriptionA" "221 DescriptionB" "221 Measure")
  250.         ("222 DescriptionA" "222 DescriptionB" "222 Measure")
  251.       )
  252.     )
  253.   )
  254. )
  255. |;
  256.  

HTH. , M.R.
Title: Re: Puzzle: flat list to nested indexed list
Post by: ribarm on December 11, 2017, 05:19:44 AM
I am just bringing attention back to this revision if you haven't noticed... I added function (fooo lst) that may be just more appropriate for your task of grouping flattened list of lists into single assoc list of lists...

https://www.theswamp.org/index.php?topic=53706.msg584004#msg584004

Regards, M.R.
 :-)
Title: Re: Puzzle: flat list to nested indexed list
Post by: Marc'Antonio Alessi on December 11, 2017, 05:43:19 AM

I am just bringing attention back to this revision if you haven't noticed... I added function (fooo lst) that may be just more appropriate for your task of grouping flattened list of lists into single assoc list of lists...

https://www.theswamp.org/index.php?topic=53706.msg584004#msg584004 (https://www.theswamp.org/index.php?topic=53706.msg584004#msg584004)

Regards, M.R.
 :)
Ok thanks for your precision and your time.  :) :) :)
Title: Re: Puzzle: flat list to nested indexed list
Post by: ribarm on December 11, 2017, 08:09:04 AM

I am just bringing attention back to this revision if you haven't noticed... I added function (fooo lst) that may be just more appropriate for your task of grouping flattened list of lists into single assoc list of lists...

https://www.theswamp.org/index.php?topic=53706.msg584004#msg584004 (https://www.theswamp.org/index.php?topic=53706.msg584004#msg584004)

Regards, M.R.
 :)
Ok thanks for your precision and your time.  :) :) :)

Thanks for thanks...
I've changed (fooo lst) again - removed (if (car lst) ... check... It seems that now it does what should when reference list is :
((0 1 2 3) (0 1 2 3 4) (0 1 2 3 4 5))... I think the result should be like it is written : ((0 (1 (2 (3 (nil nil) (4 nil (5))))))), but still not 100% sure...
Title: Re: Puzzle: flat list to nested indexed list
Post by: Marc'Antonio Alessi on December 11, 2017, 08:33:34 AM
OK  :yes: :)
Title: Re: Puzzle: flat list to nested indexed list
Post by: ribarm on December 11, 2017, 08:58:30 AM
To avoid such things (nil)s as result of unequal lengths of lists, make sure all lists are with the same lengths...  :-o

Code: [Select]
(not (vl-some 'atom lst))
=> T

(vl-every '(lambda ( x ) (= (length x) (length (car lst)))) (cdr lst))
=> T

 :yes: