Jump to content

How to repeat through numbered variable names?


gschmidt

Recommended Posts

Hi,

 

I have 3 Lists (List1, List2 and List3) filled with values

With a repeat I want to get the first element from each list.

 

I tried this:

(setq cnt 1)
(repeat 3
   (progn
      (nth 0 (strcat "List" (itoa cnt)))
      "do something"
   )
   (setq cnt (1+cnt)
)

 

But I get an error

What am i doing wrong?

Link to comment
Share on other sites

They represent distance values from an excel sheet. Also cells with empty values (nil)

I forgot to mention the (if not equal to nil) part

 

List1 (660.0 400.0 580.0 480.0 410.0 nil nil nil nil nil)

List2 (530.0 540.0 nil nil nil nil nil nil nil nil)

List3 (nil nil nil nil nil nil nil nil nil nil) ;;;;; This not always the case!

 

(setq cnt 1)
(repeat 3
  (if (/= (nth (strcat "List" (itoa cnt)) nil)
     (progn
        (nth 0 (strcat "List" (itoa cnt)))
        "do something"
     )
     (setq cnt (1+cnt)
  )
)

Link to comment
Share on other sites

This one with repeat as you looking for .

 

(setq List1 '(660.0 400.0 580.0 480.0 410.0 nil nil nil nil nil)
     List2 '(530.0 540.0 nil nil nil nil nil nil nil nil)
     List3 '(nil nil nil nil nil nil nil nil nil nil)
     )

(setq cnt 0)
(repeat 3
 (if (setq v (car (eval (read (strcat "List" (itoa (setq cnt (1+ cnt))))))))
   (setq lst (cons v lst))
     )
)

Link to comment
Share on other sites

If I've understood what you are looking to achieve, would this help?

(setq List1 '(660.0 400.0 580.0 480.0 410.0 nil nil nil nil nil)
     List2 '(530.0 540.0 nil nil nil nil nil nil nil nil)
     List3 '(nil nil nil nil nil nil nil nil nil nil)
)
(mapcar 'list list1 list2 list3)
=> ((660.0 530.0 nil) (400.0 540.0 nil) (580.0 nil nil) (480.0 nil nil) (410.0 nil nil) (nil nil nil) (nil nil nil) (nil nil nil) (nil nil nil) (nil nil nil))

Link to comment
Share on other sites

You can also get at any item in the list by using (Nth x list) obviously if X is greater than number of items you will get an error. Handy when you want to manipulate multiple lists.

Link to comment
Share on other sites

I understand that your question is about the repeat function maybe because of how your lists is currently setup.

 

If you are trying to create a data storage from an undetermined number of source. i would suggest using just one variable with multiple lists [as Bigal suggested] and also avoid using a nil value as an element.

 

So instead of this

 

(setq [b]List1[/b] '(660.0 400.0 580.0 480.0 410.0 nil nil nil nil nil)
     [b]List2[/b] '(530.0 540.0 nil nil nil nil nil nil nil nil)
     [b]List3[/b] '(nil nil nil nil nil nil nil nil nil nil)
     )

 

(setq [b]Lists[/b] '((660.0 400.0 580.0 480.0 410.0 0.0 0.0 0.0 0.0 0.0)
             (530.0 540.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0)
             (0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0)))

 

Number of elements per list is known : Excel columns / rows so there is no danger of going over the number of elements per list when you call the sub_function below.

 

(defun _nth (n l) (mapcar '(lambda (x)(nth n x)) l))

 

Say you need the 4th item of every list

 

(_nth 3 lists)
(480.0 0.0 0.0)

 

First item of every list

(_nth 0 lists)
(660.0 530.0 0.0)

 

(defun c:demo  ( / Lists op order propertydata)
     
;;   this is the section where you retreive the excel data from excel file	;;

; And this is the result ;
     (setq Lists '((660.0 400.0 580.0 480.0 410.0 0.0 0.0 0.0 0.0 0.0)
             (530.0 540.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0)
             (0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0)
             ))
     
     (initget 1 "D W V")
     (setq op (getkword "\nChoose properties [Distance/Width/Volume]: "))
     (setq order (cadr (assoc op '(("D" 0)("W" 1)("V" 2)))))
     (print (setq propertydata (_nth order Lists)))
     
; example of getting the sum of the data property
     
     (print (apply '+ propertydata))
     (princ)
     )

 

Command:  DEMO
Choose properties [[b]Distance[/b]/Width/Volume]: [b]D[/b]
(660.0 530.0 0.0)
[b]1190.0[/b]

 

HTH

Edited by pBe
Link to comment
Share on other sites

Sorry for my late respond, but my problem was solved by just a part of Tharwats code.

I have to draw 10 lines and on each line also max. 3 circles.

These values are in an excel sheet:

 

col1= name

col2= x

col3= y

col4= circle1 (can be empty)

col5= circle2 (can be empty)

col6= circle3 (can be empty)

 

(I have more colums but not mentioned here)

 

Since I have 10 lines, there are 10 rows in excel.

With a code I found on the internet, I import each column of the Excelsheet and put it in a List.

 

I used the length of values of col1 (list0) in a repeat, and a counter to ENTMAKE the lines List1(col2) and List2(col3). However the ENTMAKE of the circles (List3, List4 and List5) needs to be in a second repeat and a counter because they belong to one line.

 

When I used : (nth cnt1 (strcat "List" (itoa cnt2))) it gave an error.

But When I used : (nth cnt1 (eval (read (strcat "List" (itoa cnt2))))) it worked

Link to comment
Share on other sites

Is there a question on your previous post gschmidt? If you don't mind sharing your code here, perhaps we can make better suggestions. :)

Link to comment
Share on other sites

Pbe good example just a quick one (nth 0 lst) 1st one is it not ?

 

You know what. you are right, when i wrote the example code i initially had it to prompt for a number, and to avoid confusion i had it start at 1 rather than 0 [index 0].

 

Since the prompt for number is gone i should have revert it back to 0 then :lol: good catch BIGAL

Link to comment
Share on other sites

pBe, although i already had my answer i felt the need to respond to all the other advise. I see what u mean with the lists approache. I am a beginner (obviously for you ;-). Indeed i have a piece of code where i put the xls data in seperate lists. At this moment my code is so large it certainly needs to be more efficiënt. I will look over my code with respect to your example.

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