Jump to content




Variable names with variables in them


  • You cannot reply to this topic
10 replies to this topic

#1 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 30 October 2012 - 10:15 PM

I need help doing what the title says. I'm doing stuff like x"..y.." = "#" but it just gives my an error. Solutions?

#2 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 30 October 2012 - 10:26 PM

Besides that you got concatenation wrong, it's also9 ot possible that way. Easiest and cleanest way to do this is using_tables.

proper concatenation:
x.."y"
instead of:
x"..y.."

And for tables:
vars = {}  -- the table
x='bla'
vars[x..'y'] = 'stuff'
print( vars[x..'y'] )


#3 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 30 October 2012 - 10:41 PM

How do I add more to that table too and even READ that table, though? -.-
And it doesn't work. I just put in that 2 times, changing the varubles to what I needed, and the vars varuble was diffrent the second time, and it's giving me attempt to concentrate nil and string.

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 30 October 2012 - 10:49 PM

We can't help you debug your code without seeing your code.

#5 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 30 October 2012 - 10:50 PM

That means that your variable wasn't declared yet. Try my example, it works. 'vars' is the table you put variables into. then 'x' is declared as the string 'bla' a variable that you combine with something else the define the variable name. Then with "vars[x..'y']='test' " you put the string 'test' in the table at the key 'blay'. Then with print(vars[x..'y']) you print what's at that key.

#6 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 30 October 2012 - 10:51 PM

In english? Edit: re-read a few times and understood.
I already have my varuble declaired as "1"
I'm just gona put some of the code here:
term.clear() term.setCursorPos(1,1)
isx="1"
--game: tower defence
--FUNCTIONS
fs.delete("TDGchache") ;
function write(text)
term.write(text)
end
function newTower( x, y )
term.setCursorPos(x,y)
write("T")
--TABLES and USEABLE version
tower = "tower"
towsx = {}
towsx[tower..'isx'] = '"..x.."'
towsy = {}
towsy[tower..'isx'] = '"..y.."'
isx="isx+1"

--
--[[towx.."isx" = ""..x..""
towy.."isx" = ""..y..""
isx = "isx + 1"--]]
--[[addt = fs.open("TDGchache", "a") ;
addt.writeLine(""..x.." "..y.."")
addt.close()--]]
menuClicks()
end
function placeTower()
_, button, o, t = os.pullEvent ("mouse_click") ;
if t ==10 then
placeTower()
elseif t ==11 then
placeTower()
elseif t ==12 then
placeTower()
elseif t ==19 then
placeTower()
elseif t ==18 then
placeTower()



else
newTower( o, t ) ;
end
end
Put --changed after a line you made changes in.
ANOTHER EDIT:
I put in "tower = "tower"" and it seems to work, but I havn't tested if it actually got the varuble.
YET ANOTHER EDIT!!!:
I had it print what it's getting, it just gets "..x.." I'ved changed the code above to the code I'm using.

#7 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 30 October 2012 - 11:03 PM

Anyone?

#8 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 30 October 2012 - 11:17 PM

HECK yes I solved it. I just had to make '"..x.."' to x and it worked. LOL.
'
Off topic:
I just released that there's a piece of my code that I can't hardly understand. I pretty much put it there by accident, but hey, it works! :P/>

#9 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 30 October 2012 - 11:25 PM

Now how do I get the charts with isx being 1 when the last written one had isx being 2? Because if I can't, then I need a diffrent code. It has to be able to remeber the old ones.

#10 Geforce Fan

  • Members
  • 846 posts
  • LocationMissouri, United States, America, Earth, Solar System, Milky Way, Universe 42B, Life Street, Multiverse, 4th Dimension

Posted 30 October 2012 - 11:33 PM

It doesn't even remeber the old stuff.
If I set isx to 1, it just gives me the latest even though with the latest isx was 2. Help

#11 Orwell

    Self-Destructive

  • Members
  • 1,091 posts

Posted 31 October 2012 - 01:41 AM

You know that isx = "isx+1" does not add isx and 1, right? It just makes it a string with the value of "isx+1". That might definitely be your problem.

Just declare isx like this:
local isx = 1
and increment it like this:
isx = isx + 1






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users