How do I use external files as tables in Lua?












1















I want to make a program that chooses a random monster from a list, give the user a list of usable weapons, and allows them to choose which weapon to use. I want to use external files to store data for the weapons instead of adding the tables in the Lua file itself. I have tried using Lua files to store the data as a table.



I have a file called sword.lua in the same folder as the program I want to access it's information. It contains



sword = {'sword', '10', '1', '100'}


I am trying to access the information using



wep = io.open("sword.lua", "r")
print(wep:read("*a"))
print(wep[1])


The first print returns all of the text in the file which is



"sword = {'sword', '10', '1', '100'}" 


and the second is supposed to return the first item in the table. Every time I do this, I get a nil value from the second print. The file is being read, as indicated by the first print listing the text, but how do I make it read the file as a table that I can use in my program.










share|improve this question

























  • "the second is supposed to return the first item in the table" Why? Why would you expect printing a file (that's what wep is) to return an item in a table described by a string stored in that file?

    – Nicol Bolas
    Dec 31 '18 at 21:13


















1















I want to make a program that chooses a random monster from a list, give the user a list of usable weapons, and allows them to choose which weapon to use. I want to use external files to store data for the weapons instead of adding the tables in the Lua file itself. I have tried using Lua files to store the data as a table.



I have a file called sword.lua in the same folder as the program I want to access it's information. It contains



sword = {'sword', '10', '1', '100'}


I am trying to access the information using



wep = io.open("sword.lua", "r")
print(wep:read("*a"))
print(wep[1])


The first print returns all of the text in the file which is



"sword = {'sword', '10', '1', '100'}" 


and the second is supposed to return the first item in the table. Every time I do this, I get a nil value from the second print. The file is being read, as indicated by the first print listing the text, but how do I make it read the file as a table that I can use in my program.










share|improve this question

























  • "the second is supposed to return the first item in the table" Why? Why would you expect printing a file (that's what wep is) to return an item in a table described by a string stored in that file?

    – Nicol Bolas
    Dec 31 '18 at 21:13
















1












1








1








I want to make a program that chooses a random monster from a list, give the user a list of usable weapons, and allows them to choose which weapon to use. I want to use external files to store data for the weapons instead of adding the tables in the Lua file itself. I have tried using Lua files to store the data as a table.



I have a file called sword.lua in the same folder as the program I want to access it's information. It contains



sword = {'sword', '10', '1', '100'}


I am trying to access the information using



wep = io.open("sword.lua", "r")
print(wep:read("*a"))
print(wep[1])


The first print returns all of the text in the file which is



"sword = {'sword', '10', '1', '100'}" 


and the second is supposed to return the first item in the table. Every time I do this, I get a nil value from the second print. The file is being read, as indicated by the first print listing the text, but how do I make it read the file as a table that I can use in my program.










share|improve this question
















I want to make a program that chooses a random monster from a list, give the user a list of usable weapons, and allows them to choose which weapon to use. I want to use external files to store data for the weapons instead of adding the tables in the Lua file itself. I have tried using Lua files to store the data as a table.



I have a file called sword.lua in the same folder as the program I want to access it's information. It contains



sword = {'sword', '10', '1', '100'}


I am trying to access the information using



wep = io.open("sword.lua", "r")
print(wep:read("*a"))
print(wep[1])


The first print returns all of the text in the file which is



"sword = {'sword', '10', '1', '100'}" 


and the second is supposed to return the first item in the table. Every time I do this, I get a nil value from the second print. The file is being read, as indicated by the first print listing the text, but how do I make it read the file as a table that I can use in my program.







lua lua-table






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 1 at 0:44









braaterAfrikaaner

1,085517




1,085517










asked Dec 31 '18 at 20:47









Omega HenryOmega Henry

83




83













  • "the second is supposed to return the first item in the table" Why? Why would you expect printing a file (that's what wep is) to return an item in a table described by a string stored in that file?

    – Nicol Bolas
    Dec 31 '18 at 21:13





















  • "the second is supposed to return the first item in the table" Why? Why would you expect printing a file (that's what wep is) to return an item in a table described by a string stored in that file?

    – Nicol Bolas
    Dec 31 '18 at 21:13



















"the second is supposed to return the first item in the table" Why? Why would you expect printing a file (that's what wep is) to return an item in a table described by a string stored in that file?

– Nicol Bolas
Dec 31 '18 at 21:13







"the second is supposed to return the first item in the table" Why? Why would you expect printing a file (that's what wep is) to return an item in a table described by a string stored in that file?

– Nicol Bolas
Dec 31 '18 at 21:13














1 Answer
1






active

oldest

votes


















3














To load a table from a file use the require function. For example, save



return { 'sword', '10', '1', '100' }


as sword.lua. Why do I just use return instead of assigning to a variable? It's because that is much more flexible. If I assign the table to the variable sword inside the file, I'm sort of locked into that naming convention and furthermore I pollute the global namespace, making name collisions more likely.



With the above solution I can also assign to a local variable, like so



local sword = require("sword")
print(table.concat(sword,", "))


Another advantage is that calls to require are cached, i.e. even if you require("sword") many times, you only pay the cost for loading once. But keep in mind that because of caching you always get a handle to the same table, i.e. if you alter the table returned from require("sword"), these modifications will be shared by all instances.



Example on Wandbox






share|improve this answer
























  • Thank you so much! That does exactly what I need it to.

    – Omega Henry
    Jan 1 at 0:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991297%2fhow-do-i-use-external-files-as-tables-in-lua%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














To load a table from a file use the require function. For example, save



return { 'sword', '10', '1', '100' }


as sword.lua. Why do I just use return instead of assigning to a variable? It's because that is much more flexible. If I assign the table to the variable sword inside the file, I'm sort of locked into that naming convention and furthermore I pollute the global namespace, making name collisions more likely.



With the above solution I can also assign to a local variable, like so



local sword = require("sword")
print(table.concat(sword,", "))


Another advantage is that calls to require are cached, i.e. even if you require("sword") many times, you only pay the cost for loading once. But keep in mind that because of caching you always get a handle to the same table, i.e. if you alter the table returned from require("sword"), these modifications will be shared by all instances.



Example on Wandbox






share|improve this answer
























  • Thank you so much! That does exactly what I need it to.

    – Omega Henry
    Jan 1 at 0:57
















3














To load a table from a file use the require function. For example, save



return { 'sword', '10', '1', '100' }


as sword.lua. Why do I just use return instead of assigning to a variable? It's because that is much more flexible. If I assign the table to the variable sword inside the file, I'm sort of locked into that naming convention and furthermore I pollute the global namespace, making name collisions more likely.



With the above solution I can also assign to a local variable, like so



local sword = require("sword")
print(table.concat(sword,", "))


Another advantage is that calls to require are cached, i.e. even if you require("sword") many times, you only pay the cost for loading once. But keep in mind that because of caching you always get a handle to the same table, i.e. if you alter the table returned from require("sword"), these modifications will be shared by all instances.



Example on Wandbox






share|improve this answer
























  • Thank you so much! That does exactly what I need it to.

    – Omega Henry
    Jan 1 at 0:57














3












3








3







To load a table from a file use the require function. For example, save



return { 'sword', '10', '1', '100' }


as sword.lua. Why do I just use return instead of assigning to a variable? It's because that is much more flexible. If I assign the table to the variable sword inside the file, I'm sort of locked into that naming convention and furthermore I pollute the global namespace, making name collisions more likely.



With the above solution I can also assign to a local variable, like so



local sword = require("sword")
print(table.concat(sword,", "))


Another advantage is that calls to require are cached, i.e. even if you require("sword") many times, you only pay the cost for loading once. But keep in mind that because of caching you always get a handle to the same table, i.e. if you alter the table returned from require("sword"), these modifications will be shared by all instances.



Example on Wandbox






share|improve this answer













To load a table from a file use the require function. For example, save



return { 'sword', '10', '1', '100' }


as sword.lua. Why do I just use return instead of assigning to a variable? It's because that is much more flexible. If I assign the table to the variable sword inside the file, I'm sort of locked into that naming convention and furthermore I pollute the global namespace, making name collisions more likely.



With the above solution I can also assign to a local variable, like so



local sword = require("sword")
print(table.concat(sword,", "))


Another advantage is that calls to require are cached, i.e. even if you require("sword") many times, you only pay the cost for loading once. But keep in mind that because of caching you always get a handle to the same table, i.e. if you alter the table returned from require("sword"), these modifications will be shared by all instances.



Example on Wandbox







share|improve this answer












share|improve this answer



share|improve this answer










answered Dec 31 '18 at 21:36









Henri MenkeHenri Menke

7,61511327




7,61511327













  • Thank you so much! That does exactly what I need it to.

    – Omega Henry
    Jan 1 at 0:57



















  • Thank you so much! That does exactly what I need it to.

    – Omega Henry
    Jan 1 at 0:57

















Thank you so much! That does exactly what I need it to.

– Omega Henry
Jan 1 at 0:57





Thank you so much! That does exactly what I need it to.

– Omega Henry
Jan 1 at 0:57




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53991297%2fhow-do-i-use-external-files-as-tables-in-lua%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'