Jump to content

revising a list


Lt Dan's legs

Recommended Posts

Quick and dirty...

 

(defun test (lst / temp newlist)
 (foreach x (reverse lst)
   (if (numberp x)
     (setq temp (not (setq newlist (cons (list x temp) newlist))))
     (setq temp (cons x temp))
   )
 )
 newlist
)

Link to comment
Share on other sites

I have a list like this

(1 "A" "B" "C" 2 "A" "B")

 

How would I go about getting ((1 . ("A" "B" "C"))(2 .("A" "B"))) ?

 

 

 

 

Here's my attempt

 

(defun test (ls / a b c)
(foreach j ls
  (if (numberp j)

          (if a
                 (setq c (cons
                  (vl-list* (reverse b) a) c) a j b nil)
           (setq a j b nil))
          (setq b (cons j b))
          )

 )
(setq   newllist (reverse (cons (vl-list* (reverse b) a) c)))
)

 

 

 
(test '(1 "A" "B" "C" 2 "A" "B"))

((("A" "B" "C") . 1) (("A" "B") . 2))

 

But for the love of me, i cant revese/construct the dotted pair to match the result Lt. Dan's legs request

 

((1 . ("A" "B" "C"))(2 .("A" "B")))

 

 

Alanjt

 

I would have done it similar to your code but yours ended up with a non dotted pair..

 

 

any thoughts?

 

glad the forum is up and running again

 

:D

Link to comment
Share on other sites

But for the love of me, i cant revese/construct the dotted pair to match the result Lt. Dan's legs request

 

((1 . ("A" "B" "C"))(2 .("A" "B")))

 

Please don't forget that the second item of a dotted pair should be an atom, so the OP's request is a construction that isn't supported in AutoLISP.

 

Regards,

Mircea

Link to comment
Share on other sites

Please don't forget that the second item of a dotted pair should be an atom, so the OP's request is a construction that isn't supported in AutoLISP.

 

Regards,

Mircea

And there's no point. You can still assoc a non-dotted pair list.

 

eg. (using my sub)

 

Command: (assoc 2 (test '(1 "A" "B" "C" "D" 2 "D" "F" "R" 5 "D" "E" "T")))
(2 ("D" "F" "R"))

Link to comment
Share on other sites

And there's no point. You can still assoc a non-dotted pair list.

 

eg. (using my sub)

 

Command: (assoc 2 (test '(1 "A" "B" "C" "D" 2 "D" "F" "R" 5 "D" "E" "T")))
(2 ("D" "F" "R"))

 

I’m afraid it has to do with the consistency of accessing data stored in key & value style while still preserving the data type of the "value" (see associated lists):

 

 
 (cdr '(2 . 1))       will return   1
 (cdr '(2 1 2 3))     will return   '(1 2 3)
 (cdr '(2 (1 2 3)))   will return   '((1 2 3))

 

Regards,

Mircea

Link to comment
Share on other sites

I’m afraid it has to do with the consistency of accessing data stored in key & value style while still preserving the data type of the "value" (see associated lists):

 

 
 (cdr '(2 . 1))       will return   1
 (cdr '(2 1 2 3))     will return   '(1 2 3)
 (cdr '(2 (1 2 3)))   will return   '((1 2 3))

 

Regards,

Mircea

Just use cadr instead of cdr to retrieve the sublist.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...