Jump to content

Incrementing through s LIST


Andrew1979

Recommended Posts

I have spent the last day working some things out with lists and blocks and have made a lot of progress.

 

The problem I can't seem to work out is how to increment through a LIST of text strings. I sort of thought I had it working using Nth command, but that doesn't seem to be working as I get an error at the end of my program as I think the program hasn't realised its finished.

 

What my program does is update Block Attributes for a Title Block. That all works. It updates only a block for each paperspace selected.

 

What happens is the command CTAB cracks it when the list runs out. I am using a while loop that doesn't seem to work as anticipated.

 

Here is a snippet of the offending code:

     [color=red]([/color][color=royalblue]while[/color][color=red] ([/color][color=royalblue]>[/color] ListNumber [color=seagreen]0[/color][color=red])[/color]

  [color=red] ([/color][color=royalblue]setq[/color] nthretlist [color=red]([/color][color=royalblue]nth[/color] no retlist2[color=red]))[/color]

   [color=red]([/color][color=royalblue]setavr[/color] [color=magenta]"CTAB"[/color] nthretlist[color=red])[/color] [color=gray];Error Happens Here After Running Through Loop[/color]

   [color=red]([/color][color=royalblue]setq[/color] no [color=red]([color=royalblue]+[/color][/color] no [color=seagreen]1[/color][color=red]))[/color]
   [color=red]([/color][color=royalblue]setq [/color]ListNumber [color=red]([/color]- ListNumber 1[color=red]))[/color]

 

THis is the error:

; error: AutoCAD variable setting rejected: "CTAB" nil

Link to comment
Share on other sites

Never mind. After hours of mucking about, I added the following IF loop so the code now looks like this:

 

EDIT: Ok, now the Error is appearing again.....any thoughts?

 

     (while (> ListNumber 0)

   (setq nthretlist (nth no retlist2))

[color=green]    (if (= nthretlist nil)
     (princ "Done");(exit)
     )[/color]

   (setvar "CTAB" nthretlist)

   (setq no (+ no 1))
   (setq ListNumber (- ListNumber 1))

Edited by Andrew1979
Link to comment
Share on other sites

Andrew,

 

Post the list(s) you are using.

 

Seems to me a foreach loop would be better than a while loop

for your case.

 

The exit with an if is a bad idea.

 

ymg

Link to comment
Share on other sites

Agree with Tombu if playing with layouts use the for layoutlist, be carefull with lists (length list) returns the number of items but the first is 0 as it appears you are aware so last is -1 length

 

(setq x (length list))
(repeat (length list)
(setq val (nth (setq x (- x 1)) list) ; this way hit 0
)

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...