TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: domenicomaria on April 25, 2021, 06:56:11 AM

Title: FLAT LIST > TREE LIST
Post by: domenicomaria on April 25, 2021, 06:56:11 AM
I have this kind of flat list
(setq flat-lst
  '("1" "2" "3" "4" "5" "6" "<7" "8" "9" "<10" "11>" "12" "<13" "14" "15" "16>" "17" "<18" "19>" "20" "21" "22" "23>" "24" "25" "<26" "27" "28" "29>" "30")
)

And I need to convert it in the following format :
'("1"   "2" "3" "4" "5""6"
      ("<7"
             "8" "9"
            ("<10" "11>")
            "12"
            ("<13" "14" "15" "16>")
            "17"
            ("<18" "19>")
            "20" "21" "22" "23>"
      )
      "24" "25"
      ("<26" "27" "28" "29>")
      "30"
 )


When there is "<..." I have to begin a new sub list
When there is "...>" I have to close the more nested current  sub list
When there is a normal item,
I have to put it in the current sub list if it exists,
or if it doesn't exists, i have to leave it in the main list.

In other words, in a list,
when I find a special code I have to begin a sub-list
and I have to close it until I find another special code.

I guess the solution is a recursive function,
but my brain, right now, isn't quite capable
. . .
It seems to be simple, but it is not so easy.
. . .
I tried, but i got bad results . . .
. . .
any idea ?
. . .
Title: Re: FLAT LIST > TREE LIST
Post by: VovKa on April 25, 2021, 08:01:19 AM
Code: [Select]
(read
  (strcat "("
  (apply 'strcat
(mapcar (function (lambda (s)
     (strcat (if (= (substr s 1 1) "<")
       "("
       ""
     )
     (vl-prin1-to-string s)
     (if (= (substr s (strlen s)) ">")
       ")"
       ""
     )
     )
   )
)
flat-lst
)
  )
  ")"
  )
)
Title: Re: FLAT LIST > TREE LIST
Post by: domenicomaria on April 25, 2021, 10:59:13 AM
Code: [Select]
(read
  (strcat "("
  (apply 'strcat
(mapcar (function (lambda (s)
     (strcat (if (= (substr s 1 1) "<")
       "("
       ""
     )
     (vl-prin1-to-string s)
     (if (= (substr s (strlen s)) ">")
       ")"
       ""
     )
     )
   )
)
flat-lst
)
  )
  ")"
  )
)

I was sure you would answer. . .
. . . and that, as always, you would have solved everything.

I've thought about your method.
read , vl-prin1-to-string . . .
but i thought i wanted to solve the problem differently.

And I succeeded.
But only for lists that have no more than two nesting levels.

And for what I have to do, for the problem I have to solve, both your solution and mine are fine.

But I would be curious if it is possible to do,
without using read and vl-prin1-to-string
but more or less with my method, a function,
probably RECURSIVE,
which also works well with all nesting levels.

But my brain is burned.

At least for today.

Thank you.

You are always kind.

And you are always very clever.

ciao.
Title: Re: FLAT LIST > TREE LIST
Post by: domenicomaria on April 25, 2021, 11:01:59 AM
this is my code :

Code - Auto/Visual Lisp: [Select]
  1. (defun :FLAT-LIST>TREE-LIST-3 (flat-list / e-item e-sub-lst i-item i-sub-lst item kwe kwi)
  2.    (while
  3.       (setq item (car flat-list) )
  4.       (cond
  5.          (   (wcmatch item "*<*")
  6.             (setq e-sub-lst (list item) flat-list (cdr flat-list) kwe t)
  7.              (while (and kwe (setq e-item (car flat-list) ) )
  8.                (cond
  9.                   ;  
  10.                   (   (wcmatch e-item "*<*")
  11.                       (setq i-sub-lst (list e-item) flat-list (cdr flat-list) kwi t)
  12.                      ;                                                            -
  13.                       (while (and kwi (setq i-item (car flat-list) ) )
  14.                         (cond
  15.                            (   (wcmatch i-item "*>*")
  16.                                (setq i-sub-lst (append i-sub-lst (list i-item) )
  17.                                     e-sub-lst (append e-sub-lst (list i-sub-lst) )
  18.                                     flat-list (cdr flat-list)
  19.                                     kwi nil
  20.                               )
  21.                            )
  22.                            (   t
  23.                                (setq i-sub-lst (append i-sub-lst (list i-item) )
  24.                                     flat-list (cdr flat-list)
  25.                               )
  26.                            )
  27.                         )
  28.                      )
  29.                   )
  30.                   ;  
  31.                   (   (wcmatch e-item "*>*")
  32.                       (setq   e-sub-lst (append e-sub-lst (list e-item) )
  33.                            r          (append r (list e-sub-lst) )
  34.                            flat-list (cdr flat-list)
  35.                            kwe        nil
  36.                      )
  37.                   )
  38.                   ;  
  39.                   (   t
  40.                       (setq e-sub-lst (append e-sub-lst (list e-item) )
  41.                            flat-list (cdr flat-list)
  42.                      )
  43.                   )
  44.                )
  45.             )
  46.          )
  47.          
  48.          (   t
  49.              (setq r   (append r (list item) )
  50.                   flat-list (cdr flat-list)
  51.             )
  52.          )
  53.       )
  54.    )
  55.    r
  56. )
  57.  
  58.  
  59. (setq r nil)
  60. (setq flat-list  '("1" "2" "3" "4" "5" "6" "<7" "8" "9" "<10" "11>" "12" "<13" "14" "15" "16>" "17" "<18" "19>" "20" "21" "22" "23>" "24" "25" "<26" "27" "28" "29>" "30") )
  61. (setq r (:FLAT-LIST>TREE-LIST-3 flat-list) )
  62.  
  63.  
  64. (   "1" "2" "3" "4" "5" "6"
  65.    ( "<7" "8" "9"
  66.      ("<10" "11>")
  67.      "12"
  68.      ("<13" "14" "15" "16>")
  69.      "17"
  70.      ("<18" "19>")
  71.      "20" "21" "22" "23>"
  72.    )
  73.    "24" "25"
  74.    ("<26" "27" "28" "29>")
  75.    "30"
  76. )
  77.  

And it works.
Title: Re: FLAT LIST > TREE LIST
Post by: domenicomaria on April 26, 2021, 03:12:40 PM
Code - Auto/Visual Lisp: [Select]
  1. (defun VOVKA:FLAT-LIST>TREE-LIST (flat-list)
  2.    (read
  3.      (strcat   "("
  4.                (apply    'strcat
  5.                         (mapcar '(lambda (s)
  6.                                     (strcat    (if (= (substr s 1 1) "<")  "(" "" )
  7.                                              (vl-prin1-to-string s)
  8.                                              (if (= (substr s (strlen s)) ">") ")" "")
  9.                                     )
  10.                                  )
  11.                                  flat-list
  12.                         )
  13.                )
  14.                  ")"
  15.       )
  16.    )
  17. )
  18.  
  19.  
  20.  
  21.  
  22. (defun  PDM:FLAT-LIST>TREE-LIST ( flat-list / close-count i item open-count r sub-lst )
  23.    (setq open-count 0 close-count 0)
  24.  
  25.    (while (setq item (car flat-list) )
  26.       (cond
  27.          (   (wcmatch item "*<*")
  28.              (setq sub-lst       (append sub-lst (list item) )
  29.                   open-count    (+ open-count 1)
  30.             )
  31.          )
  32.          (   (wcmatch item "*>*")
  33.              (setq sub-lst       (append sub-lst (list item) )
  34.                   close-count (+ close-count 1)
  35.             )
  36.              (if (= open-count close-count)
  37.                (setq r             (append r (list sub-lst) )
  38.                      sub-lst      nil
  39.                )
  40.             )
  41.          )
  42.          (   t
  43.              (if sub-lst
  44.                (setq sub-lst   (append sub-lst (list item) ) )
  45.                 (setq r         (append r (list item) ) )
  46.             )
  47.          )
  48.       )
  49.       (setq flat-list    (cdr flat-list) )
  50.    )
  51.    (mapcar    '(lambda (i)  (if (listp i) (append (list (car i))  (PDM:FLAT-LIST>TREE-LIST (cdr i) ) (list (last i) ) ) i) )   r)
  52. )
  53.  
  54.    
  55. (setq flat-list
  56.                 '("1" "2" "3" "4" "5" "6" "<7" "8" "9" "<10" "11>" "12" "<13" "14" "15" "16>" "17" "<18" "19>" "20" "21" "22" "23>"
  57.                   "<24" "25" "<26" "<27" "271" "<272" "273" "274>" "28>" "29" "30>" "31" "32>" "33" "34"
  58.                  )
  59. )
  60.  
  61. (setq r1 (VOVKA:FLAT-LIST>TREE-LIST flat-list) )
  62.  
  63. (setq r2 (PDM:FLAT-LIST>TREE-LIST     flat-list) )
  64.  
  65. (equal r1 r2) = T   !
  66.  
  67.  
  68.  
  69.    (   "1" "2" "3" "4" "5" "6"
  70.       ("<7" "8" "9"
  71.            ("<10" "11>")
  72.            "12"
  73.            ("<13" "14" "15" "16>")
  74.            "17"
  75.            ("<18" "19>")
  76.            "20" "21" "22"
  77.       "23>")
  78.  
  79.       ("<24" "25"
  80.          ("<26"
  81.             ("<27" "271"
  82.                ("<272" "273" "274>")
  83.             "28>")
  84.             "29"
  85.          "30>")
  86.          "31"
  87.       "32>")
  88.       "33"
  89.       "34"
  90.    )
  91.  
  92.  

I did it !
And it works.
And there are no limits to nested lists.

And my brain, it still works a little bit.
Title: Re: FLAT LIST > TREE LIST
Post by: VovKa on April 27, 2021, 04:59:42 AM
And my brain, it still works a little bit.
then you might also want to add support for single-item lists: ("1" "<2>" "3") ;)
Title: Re: FLAT LIST > TREE LIST
Post by: domenicomaria on April 27, 2021, 05:55:16 AM
then you might also want to add support for single-item lists: ("1" "<2>" "3") ;)

Yes.
I'll do it as soon as my brain recovers.

ciao