+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Senior Member lfe011969's Avatar
    Using
    AutoCAD 2009
    Join Date
    Apr 2010
    Location
    Newport News, VA
    Posts
    157

    Default Break up a list of numbers into groups of 2

    Registered forum members do not see this ad.

    My job took me away from AutoCAD for a good while but I'm back behind a desk and I'm really rusty with my AutoLISP.

    How can I take a list of numbers and put them of groups of two? For example, if my list is this:

    Code:
    (1 2 3 4 5 6 7 8)
    How can I get it to look like this:

    Code:
    (1 2)(3 4)(5 6)(7 8)
    or even this:

    Code:
    (1 . 2)(3 . 4)(5 . 6)(7 . 8)
    As always, any help would be appreciated.
    Lonnie

  2. #2
    Quantum Mechanic Lee Mac's Avatar
    Computer Details
    Lee Mac's Computer Details
    Operating System:
    Windows 7 Ultimate (32-bit)
    Discipline
    Multi-disciplinary
    Lee Mac's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Custom Programming / Software Customisation
    Using
    AutoCAD 2013
    Join Date
    Aug 2008
    Location
    London, England
    Posts
    15,741

    Default

    Code:
    (defun f ( l ) (if l (cons (list (car l) (cadr l)) (f (cddr l)))))
    Code:
    (defun g ( l ) (if l (cons (cons (car l) (cadr l)) (g (cddr l)))))
    Generic function.
    Lee Mac Programming

    With Mathematics there is the possibility of perfect rigour, so why settle for less?

    Just another Swamper

  3. #3
    Senior Member lfe011969's Avatar
    Using
    AutoCAD 2009
    Join Date
    Apr 2010
    Location
    Newport News, VA
    Posts
    157

    Default

    I guess I should've known you would already have a solution. Thanks!
    Lonnie

  4. #4
    Super Member David Bethel's Avatar
    Discipline
    Multi-disciplinary
    David Bethel's Discipline Details
    Discipline
    Multi-disciplinary
    Details
    Commercial Food Service
    Using
    AutoCAD pre 2000
    Join Date
    Dec 2003
    Location
    Newport News, Virginia
    Posts
    1,926

    Default

    Or maybe:

    Code:
    (defun test (l / tmp)
      (and (zerop (rem (length l) 2)
           (while (< (length l) 1)
                  (setq tmp (cons (list (car l) (cadr l)) tmp)
                  (setq l (cddr l)) 
         tmp))
    -David
    R12 (Dos) - A2K

  5. #5
    Forum Deity pBe's Avatar
    Computer Details
    pBe's Computer Details
    Operating System:
    Windows XP
    Discipline
    Construction
    pBe's Discipline Details
    Discipline
    Construction
    Details
    Camp Construction planning and details
    Using
    AutoCAD 2009
    Join Date
    Apr 2010
    Posts
    2,106

    Default

    Registered forum members do not see this ad.

    Quote Originally Posted by David Bethel View Post
    Or maybe:

    Code:
    (defun test (l / tmp)....
    -David

    something missing David? .. perhaps
    Code:
    (defun test (l / tmp)
      (and (zerop (rem (length l) 2))
           (while (> (length l) 1)
                  (setq tmp (cons (list (car l) (cadr l)) tmp)
                    l (cddr l))
                 ))
         (reverse tmp)
          )
    Code:
    (defun _relist  (l n / x ln ls)
          (if (zerop (rem (length l) n))
                (repeat (/ (setq ln (length l)) n)
                      ((lambda (j)
                             (repeat j
                                   (setq x (cons (nth (setq ln (1- ln))  l)
                                                 x) )))
                            n)
                      (setq ls (cons x ls)
                            x  nil)
                      ))
          ls)
    (_relist '(1 2 3 4 5 6 7 8 9 10) 1)
    ((1) (2) (3) (4) (5) (6) (7) (8 ) (9) (10))
    (_relist '(1 2 3 4 5 6 7 8 9 10) 2)
    ((1 2) (3 4) (5 6) (7 8 ) (9 10))
    (_relist '(1 2 3 4 5 6 7 8 9 10) 3)
    nil
    (_relist '(1 2 3 4 5 6 7 8 9 10) 4)
    nil
    (_relist '(1 2 3 4 5 6 7 8 9 10) 5)
    ((1 2 3 4 5) (6 7 8 9 10))

Similar Threads

  1. Convert integers numbers into real numbers, introducing decimal separator.
    By teknomatika in forum AutoLISP, Visual LISP & DCL
    Replies: 8
    Last Post: 3rd Sep 2010, 04:07 pm
  2. Top Five Numbers from a list
    By Hippe013 in forum AutoLISP, Visual LISP & DCL
    Replies: 5
    Last Post: 16th Aug 2010, 10:47 pm
  3. Groups inside of Groups?
    By W0504 in forum AutoCAD Drawing Management & Output
    Replies: 1
    Last Post: 7th Jul 2010, 07:21 am
  4. custom break or break settings
    By b.muqlueen in forum AutoLISP, Visual LISP & DCL
    Replies: 0
    Last Post: 2nd Apr 2008, 01:19 pm
  5. Getting a list of groups used in autocad
    By nixon202 in forum AutoLISP, Visual LISP & DCL
    Replies: 4
    Last Post: 26th Mar 2007, 02:54 pm

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts