r/ComputerCraft • u/[deleted] • Nov 01 '18
automaticly number strings, how to do it?
so, i'm trying to write a script that reads a file, and as long as it finds stuff on the next line, store it in a new string, but i cannot know how many lines there will be so i cannot declare them in advance, i need the strings that store these lines to be declared as they are needed.
it should read a line and store it in "groupdata 1", then read the next line and store it in "groupdata2", then the next line in "groupdata3", and so forth, for as long as it keeps finding lines.
how do i declare a new string if that string itself has a variable name? i want to avoid having to declare 20 group names in advance, as this would limit the amount of groups to 20.
4
u/TomatoCo Nov 02 '18
Can you post an example of your code? It sounds like you're unfamiliar with tables
1
1
u/LupoCani Nov 02 '18
It's already been suggested that you should use a table. This is probably correct, but if for some reason you really want to declare the lines as regular variables, then Lua has one of those dirty little tricks to it - even regular variables are in fact table entries.
The table in question is called _G. Whenever you declare a variable, say, lorem = "ipsum", you'll find that a corresponding entry has been created in _G, in this case, print(_G["lorem"]) would output ipsum. This goes both ways. If you declare a new table entry in _G, say, _G["dolor"] = "sit smet", then print(dolor) should output sit amet.
Thus, in order to create a new variable according to the scheme you describe, to store line n you'd just need to call
_G["groupdata" .. n] = lineNow, there are some caveats to this. In particular, Computercraft might not run programs directly in _G, in which case you'd have to retrieve the relevant table yourself. It's been a while since I touched this, and I'm honestly not sure if CC is running Lua 5.1 or 5.2 right now. In 5.1, you'd do something like env_table = getfenv(), in 5.2 I think it's env_table = _ENV.
1
u/HydrantHunter Nov 02 '18
While this approach is rather ingenious, this only works if you don't localize your variables, which you really should do - globals are rarely needed, are slower, and are easily tampered with by outside programs during runtime.
1
u/LupoCani Nov 02 '18
Certainly - I'm not saying this is a good idea by any measure, I just couldn't help but indulge a bit in Lua meta-feature exploitation, given the opportunity.
1
1
u/HydrantHunter Nov 02 '18
Here's how I might approach it...
At the beginning of the program, declare and initialize a table to hold the names:
local groupData = { }To read the file:
for name in io.lines("yourFileNameHere") do --# open the file with the names (1 name per line)
groupData[#groupData + 1] = name
endTo print the results:
for i = 1, #groupData do --# cycle through all the names
print(groupData[i]) --# print a name
endTo access individual entries after reading the file:
If you want the first you would use groupData[1]
If you want the 15th you would use groupData[15]
If you have any questions, let me know.
4
u/JackFred2 Nov 01 '18
Would using a table work for your case? i.e.
This way you can iterate through them easier.