Old 09-22-2019, 10:19 AM   #1
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 189
Default What if the name of a variable contain a variable...

For example:

Code:
local a1, a2, a3, ..., an=1, 2, 3, ..., n

for i=1, 100 do

    -- how to print(ai)

end
Is this possible?
dsyrock is offline   Reply With Quote
Old 09-22-2019, 11:03 AM   #2
Edgemeal
Human being with feelings
 
Edgemeal's Avatar
 
Join Date: Apr 2016
Location: ASU`ogacihC
Posts: 1,364
Default

I'm not sure, but wouldn't it be easier to just use a table?

Code:
  local a = {}

  for i = 1, 10 do
    a[i] = i
  end
  
  for i = 1, #a do
    print(a[i])
  end
Edgemeal is offline   Reply With Quote
Old 09-22-2019, 12:54 PM   #3
X-Raym
Human being with feelings
 
X-Raym's Avatar
 
Join Date: Apr 2013
Location: France
Posts: 6,029
Default

variable variables names isnt a very common feature. it can be handy sometimes but avoided if possible IMHO. Tables are best.
X-Raym is offline   Reply With Quote
Old 09-22-2019, 02:25 PM   #4
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 7,913
Default

At least the following approach is possible, but I would strongly recommend not doing that and just using the tables...

Code:
a1=1
a2=2
a3=3
a4=1000

for i=1, 4 do
    local f = load("print(a" .. i ..")")
    f()
end
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass, Paul(X)Stretch and λ) :
http://xenakios.wordpress.com/
Xenakios is online now   Reply With Quote
Old 09-22-2019, 03:39 PM   #5
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 2,766
Default

Global variables can be accessed through the _G table. Local variables can be enumerated using debug.getlocal. However for the current use case, using a table as in Edgemeal's example seems to be the best solution.
cfillion is online now   Reply With Quote
Old 09-22-2019, 07:39 PM   #6
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 189
Default

Thanks everyone.

Yes using table is a better idea. But the problem is, the variables are not build up by me.

Actually, I'm using Lokasenna's GUI library.

In that script, I wrote something like this

Code:
	for i=1, n do

	    local name="frame"..k

	    GUI.New(name, "Frame", {
	    some code
	    some code
	    some code
	    some code
	    some code
		})

	end
I build n frames in a loop. Then I'm going to iterate them and get some data of them, such as the width. In Lokasenna's library, the way to get the value of width is accessing the variable which name is "GUI.elms.name.w". In my case, the name become GUI.elms.frame1.w, GUI.elms.frame2.w, GUI.elms.frame3.w ... GUI.elms.framen.w. So I have to deal with the problem that I mentioned
dsyrock is offline   Reply With Quote
Old 09-22-2019, 08:36 PM   #7
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,236
Default

Code:
for i = 1, 100 do
  local w = GUI.elms["frame"..i].w
end
Lokasenna is online now   Reply With Quote
Old 09-22-2019, 08:40 PM   #8
dsyrock
Human being with feelings
 
dsyrock's Avatar
 
Join Date: Sep 2018
Location: China
Posts: 189
Default

Quote:
Originally Posted by Lokasenna View Post
Code:
for i = 1, 100 do
  local w = GUI.elms["frame"..i].w
end
Oh wow thanks Lokasenna!
dsyrock is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 03:51 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.