Generate numbered variables in loop

Generate numbered variables in loop

DC-MWA
Collaborator Collaborator
1,330 Views
9 Replies
Message 1 of 10

Generate numbered variables in loop

DC-MWA
Collaborator
Collaborator

Hi all,

I'm looking to make lists of variable values in a loop.

Each pass i need to create a new list as follows:

(setq 1-2 (list A B C))

(setq 2-3 (list A B C))

(setq 3-4 (list A B C))

(setq 4-5 (list A B C))

;end loop

Then gather all new lists,  example(1-2, 2-3, 3-4, 4-5)

 

so basically add 1 to each number on each side of "-"

 

I hope that makes sense.

0 Likes
1,331 Views
9 Replies
Replies (9)
Message 2 of 10

DC-MWA
Collaborator
Collaborator

I figured it out.

0 Likes
Message 3 of 10

devitg
Advisor
Advisor

please , show us how did you figured it out

0 Likes
Message 4 of 10

pbejse
Mentor
Mentor

@devitg wrote:

please , show us how did you figured it out


 

I'm more interested on why the need for that format.

Do you mind telling us @DC-MWA  ?

 

0 Likes
Message 5 of 10

DC-MWA
Collaborator
Collaborator

I ended up doing this (I'm sure this is long and drawn out, I'm no programmer by any means)

 

(setq bt-n 0);;start count for lists
(setq bt-n2 1);;start count for lists

(while

;;program gathers data and puts in variables

(setq bt-n (1+ bt-n))<-- Increment variable in loop
(setq bt-n2 (1+ bt-n2))<-- Increment variable in loop
(setq x_list (list (strcat (itoa bt-n) "_" (itoa bt-n2)) (/ slen 12) (/ d1 12) (/ d2 12)))
(setq master_list (append (list x_list) master_list))
(setq master_list_rev (reverse master_list))

);end while

 

;;produces

((1_2 150.0 30.0 25.0) (2_3 150.0 30.0 30.0) (3_4 50.0 15.0 15.0) (4_5 80.0 160.0 160.0) (5_6 100.0 95.0 95.0) (6_7 70.0 60.0 60.0))

 

0 Likes
Message 6 of 10

DC-MWA
Collaborator
Collaborator

I'm looking to export the information to an excel file.

Each list represents data between two points...

1_2 is the information between 1 and 2

2_3 is the information between 2 and 3

 

Then the "master list" gathers all the lists:

((1_2 150.0 30.0 25.0) (2_3 150.0 30.0 30.0) (3_4 50.0 15.0 15.0) (4_5 80.0 160.0 160.0) (5_6 100.0 95.0 95.0) (6_7 70.0 60.0 60.0))

the points are taken around a building outline.

Message 7 of 10

_gile
Consultant
Consultant

@DC-MWA wrote:

(setq x_list (list (strcat (itoa bt-n) "_" (itoa bt-n2)) (/ slen 12) (/ d1 12) (/ d2 12)))


What are slen, d1, d2?

If you do not give more precisions about ALL inputs, it will be difficult to help you.



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 8 of 10

DC-MWA
Collaborator
Collaborator

slen = segment length

d1 = distance 1

d2 = distance 2

0 Likes
Message 9 of 10

Sea-Haven
Mentor
Mentor

If you want decimal answer (/ slen 12.0)

0 Likes
Message 10 of 10

martti.halminen
Collaborator
Collaborator

Well, you just managed to create an eternal loop.

- not quite eternal, probably crashes when it runs out of memory.

 

Where is your test expression telling when to stop the WHILE loop?

 

- This doesn't work quite like you claim to intend, produces something like 

 

(("1337_1338" 4 2 2) ("1336_1337" 4 2 2) ("1335_1336" 4 2 2) ("1334_1335" 4 2 2)) i.e. the first item is a string, not a symbol.

- not necessarily a problem, can be used this way too.

 

One style hint:  

 

 (append (list x_list) master_list)

 

is unnecessarily inefficient.

 

(cons x_list master_list)

 

would produce the same end result but less garbage.

 

Generally, APPEND gets far too much mindshare in teaching Lisp, in real life it is not needed all that much.

- I am working here with a directory containing 97000 lines of AutoLISP code. There are only 12 calls to APPEND in that program.

 

-- 

 

 

 

 

 

0 Likes