Create Variables on the Fly

Create Variables on the Fly

Anonymous
Not applicable
1,118 Views
25 Replies
Message 1 of 26

Create Variables on the Fly

Anonymous
Not applicable
What Frank and Randy have shown do exactly what I need but before I go
with the big move I am interested in how this method works.

For instance both Frank and Randy showed how it could be done by
providing the snipit code how do you suppose this is going to work
without using set or read?

Thank you all very much I never knew about the set command and I am
extremely excited. \\\\Kicking myself//////

David at the CADapult
http://geocities.com/~cadapult

On Mon, 7 Feb 2000 15:16:03 -0800, "R. Robert Bell"
wrote:

>I don't think that's a good example.
>
>Michael means that (nth I MyList) or (foreach I MyList), where MyList =
>'("A" "B" "C"), is just as good as incrementing variable names (in fact,
>less work!). Do I get a gold star, Mike? There is no compelling reason for
>incrementing, dxf-style, lists, either.
0 Likes
1,119 Views
25 Replies
Replies (25)
Message 2 of 26

Anonymous
Not applicable
I want to know if it is possible to create variables on the fly inside
a program.

Lets say that I want to create 3 variables and I have a function that
will create those variables I have no problems creating them using
this method

(setq v1 "cool"
v2 "really cool"
v3 "Xtra cool"
)

Now lets say that I want to create 4 variables. I would have to
repeat the same process again, by defining v(1) v(2) v(3) and v(4)

What I would really like to do is have v(4) created on the fly. I
guess what I really want to do is setup some kind of counter that as I
loop though it generates the variable name "V" that has a strcat or
something like it to add on the counter number to it so I would end up
with (strcat "V" cnt) -> v1 is now a variable.

Can this be done? I have tried a number of ways but I keep throwing
myself in a loop (more like spaghetti) and I get all kinds of
confused.

If this doesn't make sence let me know and I will try and explain it
differently.

Thanks

David at the CADapult
http://geocities.com/~cadapult/
0 Likes
Message 3 of 26

Anonymous
Not applicable
It is possible using set, read and strcat etc., but why not rethink the dependent logic and use a
list instead?

________________________________

[email protected]
> Not < an AutoDESK classroom monitor
Imagination makes all things possible
________________________________

David Garrigues wrote in message <[email protected]>...
I want to know if it is possible to create variables on the fly inside
a program.

Lets say that I want to create 3 variables and I have a function that
will create those variables I have no problems creating them using
this method

(setq v1 "cool"
v2 "really cool"
v3 "Xtra cool"
)

Now lets say that I want to create 4 variables. I would have to
repeat the same process again, by defining v(1) v(2) v(3) and v(4)

What I would really like to do is have v(4) created on the fly. I
guess what I really want to do is setup some kind of counter that as I
loop though it generates the variable name "V" that has a strcat or
something like it to add on the counter number to it so I would end up
with (strcat "V" cnt) -> v1 is now a variable.

Can this be done? I have tried a number of ways but I keep throwing
myself in a loop (more like spaghetti) and I get all kinds of
confused.

If this doesn't make sence let me know and I will try and explain it
differently.

Thanks

David at the CADapult
http://geocities.com/~cadapult/
0 Likes
Message 4 of 26

Anonymous
Not applicable
You need to use the SET and READ functions. Here's an example. It'll create
the variables v1 through v5, setting the value to 5n (5, 10, etc.)

(setq c 0)
(repeat 5
(set (read (strcat "v" (itoa (setq c (1+ c))))) (* c 5))
)

--
Get free software and more at:
http://msnhomepages.talkcity.com/protectionfault/foquendo

"David Garrigues" wrote in message
news:[email protected]...
> I want to know if it is possible to create variables on the fly inside
> a program.
>
> Lets say that I want to create 3 variables and I have a function that
> will create those variables I have no problems creating them using
> this method
>
> (setq v1 "cool"
> v2 "really cool"
> v3 "Xtra cool"
> )
>
> Now lets say that I want to create 4 variables. I would have to
> repeat the same process again, by defining v(1) v(2) v(3) and v(4)
>
> What I would really like to do is have v(4) created on the fly. I
> guess what I really want to do is setup some kind of counter that as I
> loop though it generates the variable name "V" that has a strcat or
> something like it to add on the counter number to it so I would end up
> with (strcat "V" cnt) -> v1 is now a variable.
>
> Can this be done? I have tried a number of ways but I keep throwing
> myself in a loop (more like spaghetti) and I get all kinds of
> confused.
>
> If this doesn't make sence let me know and I will try and explain it
> differently.
>
> Thanks
>
> David at the CADapult
> http://geocities.com/~cadapult/
>
0 Likes
Message 5 of 26

Anonymous
Not applicable
(setq counter_variable 4)
(eval (read (strcat "(setq v" (itoa counter_variable) " " \"Cooler
still!\")")))
0 Likes
Message 6 of 26

Anonymous
Not applicable
Interesting idea, Michael. Could you elaborate? If I'm not sure how many
variables I need, how would you recommend approaching this?

--
Get free software and more at:
http://msnhomepages.talkcity.com/protectionfault/foquendo

"Michael Puckett" wrote in message
news:[email protected]...
> It is possible using set, read and strcat etc., but why not rethink the
dependent logic and use a
> list instead?
>
> ________________________________
>
> [email protected]
> > Not < an AutoDESK classroom monitor
> Imagination makes all things possible
> ________________________________
>
> David Garrigues wrote in message
<[email protected]>...
> I want to know if it is possible to create variables on the fly inside
> a program.
>
> Lets say that I want to create 3 variables and I have a function that
> will create those variables I have no problems creating them using
> this method
>
> (setq v1 "cool"
> v2 "really cool"
> v3 "Xtra cool"
> )
>
> Now lets say that I want to create 4 variables. I would have to
> repeat the same process again, by defining v(1) v(2) v(3) and v(4)
>
> What I would really like to do is have v(4) created on the fly. I
> guess what I really want to do is setup some kind of counter that as I
> loop though it generates the variable name "V" that has a strcat or
> something like it to add on the counter number to it so I would end up
> with (strcat "V" cnt) -> v1 is now a variable.
>
> Can this be done? I have tried a number of ways but I keep throwing
> myself in a loop (more like spaghetti) and I get all kinds of
> confused.
>
> If this doesn't make sence let me know and I will try and explain it
> differently.
>
> Thanks
>
> David at the CADapult
> http://geocities.com/~cadapult/
>
>
>
0 Likes
Message 7 of 26

Anonymous
Not applicable
Like I said, rethink the dependent logic.

Show me an example where a list won't work.

________________________________

[email protected]
> Not < an AutoDESK classroom monitor
Imagination makes all things possible
________________________________

Frank Oquendo wrote in message <[email protected]>...
Interesting idea, Michael. Could you elaborate? If I'm not sure how many
variables I need, how would you recommend approaching this?

--
Get free software and more at:
http://msnhomepages.talkcity.com/protectionfault/foquendo

"Michael Puckett" wrote in message
news:[email protected]...
> It is possible using set, read and strcat etc., but why not rethink the
dependent logic and use a
> list instead?
>
> ________________________________
>
> [email protected]
> > Not < an AutoDESK classroom monitor
> Imagination makes all things possible
> ________________________________
>
> David Garrigues wrote in message
<[email protected]>...
> I want to know if it is possible to create variables on the fly inside
> a program.
>
> Lets say that I want to create 3 variables and I have a function that
> will create those variables I have no problems creating them using
> this method
>
> (setq v1 "cool"
> v2 "really cool"
> v3 "Xtra cool"
> )
>
> Now lets say that I want to create 4 variables. I would have to
> repeat the same process again, by defining v(1) v(2) v(3) and v(4)
>
> What I would really like to do is have v(4) created on the fly. I
> guess what I really want to do is setup some kind of counter that as I
> loop though it generates the variable name "V" that has a strcat or
> something like it to add on the counter number to it so I would end up
> with (strcat "V" cnt) -> v1 is now a variable.
>
> Can this be done? I have tried a number of ways but I keep throwing
> myself in a loop (more like spaghetti) and I get all kinds of
> confused.
>
> If this doesn't make sence let me know and I will try and explain it
> differently.
>
> Thanks
>
> David at the CADapult
> http://geocities.com/~cadapult/
>
>
>
0 Likes
Message 8 of 26

Anonymous
Not applicable
I'm not questioning you Michael, I just don't see your point. I'd appreciate
you taking the time to enlighten me because I'm sure you're right but I'm
also short-sighted.

--
Get free software and more at:
http://msnhomepages.talkcity.com/protectionfault/foquendo

"Michael Puckett" wrote in message
news:[email protected]...
> Like I said, rethink the dependent logic.
>
> Show me an example where a list won't work.
>
> ________________________________
>
> [email protected]
> > Not < an AutoDESK classroom monitor
> Imagination makes all things possible
> ________________________________
>
> Frank Oquendo wrote in message <[email protected]>...
> Interesting idea, Michael. Could you elaborate? If I'm not sure how many
> variables I need, how would you recommend approaching this?
>
> --
> Get free software and more at:
> http://msnhomepages.talkcity.com/protectionfault/foquendo
>
>
> "Michael Puckett" wrote in message
> news:[email protected]...
> > It is possible using set, read and strcat etc., but why not rethink the
> dependent logic and use a
> > list instead?
> >
> > ________________________________
> >
> > [email protected]
> > > Not < an AutoDESK classroom monitor
> > Imagination makes all things possible
> > ________________________________
> >
> > David Garrigues wrote in message
> <[email protected]>...
> > I want to know if it is possible to create variables on the fly inside
> > a program.
> >
> > Lets say that I want to create 3 variables and I have a function that
> > will create those variables I have no problems creating them using
> > this method
> >
> > (setq v1 "cool"
> > v2 "really cool"
> > v3 "Xtra cool"
> > )
> >
> > Now lets say that I want to create 4 variables. I would have to
> > repeat the same process again, by defining v(1) v(2) v(3) and v(4)
> >
> > What I would really like to do is have v(4) created on the fly. I
> > guess what I really want to do is setup some kind of counter that as I
> > loop though it generates the variable name "V" that has a strcat or
> > something like it to add on the counter number to it so I would end up
> > with (strcat "V" cnt) -> v1 is now a variable.
> >
> > Can this be done? I have tried a number of ways but I keep throwing
> > myself in a loop (more like spaghetti) and I get all kinds of
> > confused.
> >
> > If this doesn't make sence let me know and I will try and explain it
> > differently.
> >
> > Thanks
> >
> > David at the CADapult
> > http://geocities.com/~cadapult/
> >
> >
> >
>
>
>
>
0 Likes
Message 9 of 26

Anonymous
Not applicable
A list is exactly what I had in mind the problem is that I am basing
everything on a function that will already be inside a function that
has a list. So what I am looking for is a way to set it.

Then if the variable exists I can call it by name in a menu.

Hey I have never used the set command so this is new territory for me
but it looks like it will work.

Thanks! I will give it a shot.
David at the CADapult
http://geocities.com/~cadapult/

On Mon, 7 Feb 2000 14:40:03 -0700, "Michael Puckett"
wrote:

>It is possible using set, read and strcat etc., but why not rethink the dependent logic and use a
>list instead?
>
>________________________________
>
>[email protected]
>> Not < an AutoDESK classroom monitor
>Imagination makes all things possible
>________________________________
0 Likes
Message 10 of 26

Anonymous
Not applicable
Why not post the code?

________________________________

[email protected]
> Not < an AutoDESK classroom monitor
Imagination makes all things possible
________________________________

David Garrigues wrote in message <[email protected]>...
A list is exactly what I had in mind the problem is that I am basing
everything on a function that will already be inside a function that
has a list. So what I am looking for is a way to set it.

Then if the variable exists I can call it by name in a menu.

Hey I have never used the set command so this is new territory for me
but it looks like it will work.

Thanks! I will give it a shot.
David at the CADapult
http://geocities.com/~cadapult/

On Mon, 7 Feb 2000 14:40:03 -0700, "Michael Puckett"
wrote:

>It is possible using set, read and strcat etc., but why not rethink the dependent logic and use a
>list instead?
>
>________________________________
>
>[email protected]
>> Not < an AutoDESK classroom monitor
>Imagination makes all things possible
>________________________________
0 Likes
Message 11 of 26

Anonymous
Not applicable
I hope you don't mind me joining in on the fun.

I had a condition where I had to create differing types of block.

So I added a modifier which would be determined based off the position,
size, angle, and rotation.

My modifier would be placed at the beginning then have the application
attach the rest.

The rest being, size and some xdata.

I had to be able to separate certain blocks and categorize.

I would have to see exactly what you're trying to accomplish to suggest
anything else though.

Should be the same principle.

--
[email protected]
"Automation Equal Imagination..." - "Automating Success"
Programmer/Developer/MIS Assist.
www.wharchitects.com

David Garrigues wrote in article
<[email protected]>...
> I want to know if it is possible to create variables on the fly inside
> a program.
>
> Lets say that I want to create 3 variables and I have a function that
> will create those variables I have no problems creating them using
> this method
>
> (setq v1 "cool"
> v2 "really cool"
> v3 "Xtra cool"
> )
>
> Now lets say that I want to create 4 variables. I would have to
> repeat the same process again, by defining v(1) v(2) v(3) and v(4)
>
> What I would really like to do is have v(4) created on the fly. I
> guess what I really want to do is setup some kind of counter that as I
> loop though it generates the variable name "V" that has a strcat or
> something like it to add on the counter number to it so I would end up
> with (strcat "V" cnt) -> v1 is now a variable.
>
> Can this be done? I have tried a number of ways but I keep throwing
> myself in a loop (more like spaghetti) and I get all kinds of
> confused.
>
> If this doesn't make sence let me know and I will try and explain it
> differently.
>
> Thanks
>
> David at the CADapult
> http://geocities.com/~cadapult/
>
>
0 Likes
Message 12 of 26

Anonymous
Not applicable
Frank Oquendo escribió en mensaje <[email protected]>...
>I'm not questioning you Michael, I just don't see your point. I'd
appreciate
>you taking the time to enlighten me because I'm sure you're right but I'm
>also short-sighted.

Just like an example:

Why do you need to create variables on-the-fly
i.e.
(eval (read (strcat "(setq v" (itoa 1) " 1)")))
(eval (read ....
etc
if you can have a list like this:

'(
("v1" . 1)
("v2" . 2)
("v3" . 3)
)

where given the "name" you have the value

(cdr (assoc "v3" mylist))

Eduardo Muñoz
0 Likes
Message 13 of 26

Anonymous
Not applicable
Ok. Now I get it. Thanks for the tip. I can see where that would come in
handy.

--
Get free software and more at:
http://msnhomepages.talkcity.com/protectionfault/foquendo

"Eduardo Muñoz" wrote in message
news:[email protected]...
> Frank Oquendo escribió en mensaje
<[email protected]>...
> >I'm not questioning you Michael, I just don't see your point. I'd
> appreciate
> >you taking the time to enlighten me because I'm sure you're right but I'm
> >also short-sighted.
>
>
> Just like an example:
>
> Why do you need to create variables on-the-fly
> i.e.
> (eval (read (strcat "(setq v" (itoa 1) " 1)")))
> (eval (read ....
> etc
> if you can have a list like this:
>
> '(
> ("v1" . 1)
> ("v2" . 2)
> ("v3" . 3)
> )
>
> where given the "name" you have the value
>
> (cdr (assoc "v3" mylist))
>
>
> Eduardo Muñoz
>
>
>
>
>
>
>
0 Likes
Message 14 of 26

Anonymous
Not applicable
David Garrigues escribió en mensaje
<[email protected]>...
>A list is exactly what I had in mind the problem is that I am basing
>everything on a function that will already be inside a function that
>has a list. So what I am looking for is a way to set it.

I don't know what you're trying
to do, but...

Even a function is a list, so you can nest it.
Example:

(defun c:test ()
(setq sOpt (getstring "\nFunction: "))
(setq a
'(
("a" . (defun c:func () (princ "\nHello a.")(princ)))
("b" . (defun c:func () (princ "\nHello b.")(princ)))
)
)
(eval (cdr (assoc sOpt a)))
(princ)
)

Command: test
Function: a
Command: func
Hello a.
Command: test
Function: b
Command: func
Hello b.

You don't need to buid code at run time via (read ....

Just trying to help

Eduardo Muñoz
0 Likes
Message 15 of 26

Anonymous
Not applicable
I don't think that's a good example.

Michael means that (nth I MyList) or (foreach I MyList), where MyList =
'("A" "B" "C"), is just as good as incrementing variable names (in fact,
less work!). Do I get a gold star, Mike? There is no compelling reason for
incrementing, dxf-style, lists, either.

--
R. Robert Bell, MCSE
Network Administrator (or, Modern-day Wizard)
(remove the "not." in my address for direct e-mail)

Eduardo Muñoz wrote in message
news:[email protected]...
> Frank Oquendo escribió en mensaje
<[email protected]>...
> >I'm not questioning you Michael, I just don't see your point. I'd
> appreciate
> >you taking the time to enlighten me because I'm sure you're right but I'm
> >also short-sighted.
>
>
> Just like an example:
>
> Why do you need to create variables on-the-fly
> i.e.
> (eval (read (strcat "(setq v" (itoa 1) " 1)")))
> (eval (read ....
> etc
> if you can have a list like this:
>
> '(
> ("v1" . 1)
> ("v2" . 2)
> ("v3" . 3)
> )
>
> where given the "name" you have the value
>
> (cdr (assoc "v3" mylist))
>
>
> Eduardo Muñoz
>
>
>
>
>
>
>
0 Likes
Message 16 of 26

Anonymous
Not applicable
*
________________________________

[email protected]
> Not < an AutoDESK classroom monitor
Imagination makes all things possible
________________________________

R. Robert Bell wrote in message <[email protected]>...
I don't think that's a good example.

Michael means that (nth I MyList) or (foreach I MyList), where MyList =
'("A" "B" "C"), is just as good as incrementing variable names (in fact,
less work!). Do I get a gold star, Mike? There is no compelling reason for
incrementing, dxf-style, lists, either.

--
R. Robert Bell, MCSE
Network Administrator (or, Modern-day Wizard)
(remove the "not." in my address for direct e-mail)

Eduardo Muñoz wrote in message
news:[email protected]...
> Frank Oquendo escribió en mensaje
<[email protected]>...
> >I'm not questioning you Michael, I just don't see your point. I'd
> appreciate
> >you taking the time to enlighten me because I'm sure you're right but I'm
> >also short-sighted.
>
>
> Just like an example:
>
> Why do you need to create variables on-the-fly
> i.e.
> (eval (read (strcat "(setq v" (itoa 1) " 1)")))
> (eval (read ....
> etc
> if you can have a list like this:
>
> '(
> ("v1" . 1)
> ("v2" . 2)
> ("v3" . 3)
> )
>
> where given the "name" you have the value
>
> (cdr (assoc "v3" mylist))
>
>
> Eduardo Muñoz
>
>
>
>
>
>
>
0 Likes
Message 17 of 26

Anonymous
Not applicable
Thanks for the star! But it wasn't gold! 😞
0 Likes
Message 18 of 26

Anonymous
Not applicable
Hmmm, was when it left here.

You must be running a MS newsreader.

________________________________

[email protected]
> Not < an AutoDESK classroom monitor
Imagination makes all things possible
________________________________

R. Robert Bell wrote in message <[email protected]>...
Thanks for the star! But it wasn't gold! 😞
0 Likes
Message 19 of 26

Anonymous
Not applicable
Ah, you caught me! How can I be a MCSE, otherwise? Bill would be really
upset with me, if I didn't. . .

--
R. Robert Bell, MCSE
Network Administrator (or, Modern-day Wizard)
(remove the "not." in my address for direct e-mail)
0 Likes
Message 20 of 26

Anonymous
Not applicable
Why not post your code?

__________________________________

Michael Puckett
[email protected]
> Not < an Autodesk hall monitor.
Imagination makes all things possible.
__________________________________

"David Garrigues" wrote in message
news:[email protected]...
What Frank and Randy have shown do exactly what I need but before I go
with the big move I am interested in how this method works.

For instance both Frank and Randy showed how it could be done by
providing the snipit code how do you suppose this is going to work
without using set or read?

Thank you all very much I never knew about the set command and I am
extremely excited. \\\\Kicking myself//////

David at the CADapult
http://geocities.com/~cadapult

On Mon, 7 Feb 2000 15:16:03 -0800, "R. Robert Bell"
wrote:

>I don't think that's a good example.
>
>Michael means that (nth I MyList) or (foreach I MyList), where MyList =
>'("A" "B" "C"), is just as good as incrementing variable names (in fact,
>less work!). Do I get a gold star, Mike? There is no compelling reason for
>incrementing, dxf-style, lists, either.
0 Likes