TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Coder on April 13, 2011, 09:03:16 AM

Title: List to be re-constucted
Post by: Coder on April 13, 2011, 09:03:16 AM
Hello .

How could I divide this list to be a list or coordinate points ?

Code: [Select]
([color=red]3.09441[/color] [color=blue]3.20876[/color] 5.01197 4.31587 6.17619 2.29938 4.34473 -0.0756835 3.25095 1.8188 1.47881 1.8188 0.888771 4.57027 3.09441 3.20876)

Example .
(10 3.09441 3.20876)

So on for the rest of list .

Thanks.
Title: Re: List to be re-constucted
Post by: Lee Mac on April 13, 2011, 09:09:17 AM
http://lee-mac.com/groupbynum.html (http://lee-mac.com/groupbynum.html)
Title: Re: List to be re-constucted
Post by: gile on April 13, 2011, 09:14:02 AM
Hi,

Here's a way:

Code: [Select]
(defun flatTo2dDXF (l)
  (if (cdr l)
    (cons (list 10 (car l) (cadr l)) (flatTo2dDXF (cddr l)))
  )
)
Title: Re: List to be re-constucted
Post by: alanjt on April 13, 2011, 09:14:57 AM
Code: [Select]
(defun foo (lst)
  (if (cddr lst)
    (cons (list 10 (car lst) (cadr lst)) (foo (cddr lst)))
  )
)
Title: Re: List to be re-constucted
Post by: alanjt on April 13, 2011, 09:15:56 AM
Well crap. I guess I should have checked when I received the warning that someone has posted.
Title: Re: List to be re-constucted
Post by: pBe on April 13, 2011, 09:19:43 AM
Code: [Select]
(defun test (lst / NewList)
(repeat (/ (length lst) 2)
   (setq NewList (cons (list 10 (car lst) (car (setq ss (cdr lst)))) NewList) )
        (setq lst (cdr lst)))
  NewList
  )

Code: [Select]
[color=blue](defun test (lst)
  (if lst (progn
  (setq New_list (cons (list 10 (car lst) (car (setq lst (cdr lst)))) New_list))
  (test (cdr lst))) New_list)
  )[/color]

Edit: modifed for what its worth  :-)
Title: Re: List to be re-constucted
Post by: MP on April 13, 2011, 09:23:22 AM
Code: [Select]
(defun [color=red]_SubLists[/color] ( lst n / sublist result )

    [color=green];;  Group list 'lst' into sublists of length 'n'.
    ;;
    ;;  Example:
    ;;
    ;;      (_SubLists '(1 2 3 4 5) 2)
    ;;
    ;;      => ((1 2) (3 4) (5))
    ;;
    ;;  Caller's responsibility to pass valid data. Dumb data /
    ;;  args? RAKE IN FACE.[/color]

    (foreach item lst
        (if (eq n (length (setq sublist (cons item sublist))))
            (setq
                result  (cons sublist result)
                sublist nil
            )
        )
    )
   
    (reverse
        (mapcar 'reverse
            (if sublist
                (cons sublist result)
                result
            )
        )
    )
)

Sample use:

Code: [Select]
(mapcar
    (function (lambda (sublist) (cons 10 sublist)))
    ([color=red]_SubLists[/color]
       '(   3.09441   
            3.20876
            5.01197
            4.31587
            6.17619
            2.29938
            4.34473
           -0.0756835
            3.25095
            1.8188
            1.47881
            1.8188
            0.888771
            4.57027
            3.09441
            3.20876
        )
        2
    )   
)

Code: [Select]
=>  (   (10 3.09441 3.20876)
        (10 5.01197 4.31587)
        (10 6.17619 2.29938)
        (10 4.34473 -0.0756835)
        (10 3.25095 1.8188)
        (10 1.47881 1.8188)
        (10 0.888771 4.57027)
        (10 3.09441 3.20876)
    )
Title: Re: List to be re-constucted
Post by: pBe on April 13, 2011, 09:29:52 AM
double crap.. mine's messed up  :realmad:
Title: Re: List to be re-constucted
Post by: Coder on April 13, 2011, 09:34:10 AM
Thanks a lot guys .

Great help . Appreciated a lot .

I should have mentioned that my list is issued from a list of Coordinates from a routine .


Code: [Select]
(setq ss (entsel "\n Poly :"))
(setq pts (vlax-get (vlax-ename->vla-object (car ss)) 'Coordinates))

So the outcome of the Coordinates must be re-constructed to be re-used for another routs of lining .

Thanks a gain
Title: Re: List to be re-constucted
Post by: Lee Mac on April 13, 2011, 09:35:39 AM
Code: [Select]
(defun f ( l n )
  (
    (lambda ( g )
      (if l
        (cons (g l n) (f l n))
      )
    )
    (lambda ( a b )
      (if (< 0 b)
        (cons (car a) (g (cdr a) (1- b)))
        (setq l a a nil)
      )
    )
  )
)

Code: [Select]
_$ (f '(1 2 3 4 5 6 7 8 9) 3)
((1 2 3) (4 5 6) (7 8 9))
Title: Re: List to be re-constucted
Post by: chlh_jd on April 13, 2011, 03:46:47 PM
Code: [Select]
(defun list-comp (a b / mid rslt)
    (repeat (/ (length a) b)
      (setq mid nil)
      (repeat b
(setq mid (cons (car a) mid)
      a   (cdr a)
)
      )
      (setq rslt (cons (reverse mid) rslt))
    )
  (if a (reverse (cons a rslt))
    (reverse rslt))
  )