I am makeing a program and i have local items = {
["dirt"] = { "$1" },
["cables"] = { "$10" }
}
How to get the keys dirt and cables etc?
5 replies to this topic
#1
Posted 01 April 2015 - 01:58 AM
#2
Posted 01 April 2015 - 02:14 AM
I believe it's pairs
for key,value in pairs(table) do print(key.." "..value) end
#3
Posted 01 April 2015 - 03:39 AM
If Lopus590s answer doesn't help your problem, you may want to perform an index operation?:
In your rather simple example, you could do the following:
PS: I'm not exactly sure, but you may want your datasets to not include the currency and have the price as a number, so you can calculate arithmetic operations against them
You can always add the currency later, removing the currency however is not so easy and consumes more time that isn't needed
If you do want to retrieve the keys available in a table, Lopus590s answer is the one to follow
Just be sure to never call your actual table you are working on "table"
print(items.dirt[1]) --#prints $1 print(items.cables[1]) --#prints $10I don't know, why you are putting them into a second layer of tables, though.
In your rather simple example, you could do the following:
local items = {
["dirt"] ="$1",
["cables"] = "$10"
}
print(items.dirt) --#prints $1
print(items.cables) --#prints $10
But maybe you want to add more properties other than the price to the item as well, then your way fits nice as it is.PS: I'm not exactly sure, but you may want your datasets to not include the currency and have the price as a number, so you can calculate arithmetic operations against them
You can always add the currency later, removing the currency however is not so easy and consumes more time that isn't needed
If you do want to retrieve the keys available in a table, Lopus590s answer is the one to follow
Edited by InDieTasten, 01 April 2015 - 03:46 AM.
#4
Posted 01 April 2015 - 04:25 PM
InDieTasten, on 01 April 2015 - 03:39 AM, said:
If Lopus590s answer doesn't help your problem, you may want to perform an index operation?:
In your rather simple example, you could do the following:
PS: I'm not exactly sure, but you may want your datasets to not include the currency and have the price as a number, so you can calculate arithmetic operations against them
You can always add the currency later, removing the currency however is not so easy and consumes more time that isn't needed
If you do want to retrieve the keys available in a table, Lopus590s answer is the one to follow
Just be sure to never call your actual table you are working on "table" 
print(items.dirt[1]) --#prints $1 print(items.cables[1]) --#prints $10I don't know, why you are putting them into a second layer of tables, though.
In your rather simple example, you could do the following:
local items = {
["dirt"] ="$1",
["cables"] = "$10"
}
print(items.dirt) --#prints $1
print(items.cables) --#prints $10
But maybe you want to add more properties other than the price to the item as well, then your way fits nice as it is.PS: I'm not exactly sure, but you may want your datasets to not include the currency and have the price as a number, so you can calculate arithmetic operations against them
You can always add the currency later, removing the currency however is not so easy and consumes more time that isn't needed
If you do want to retrieve the keys available in a table, Lopus590s answer is the one to follow
It's really simple to remove the $ from the string. Just do
local withoutCurrencySymbol = string.gsub(withCurrencySymbol,"%$","")an alternative would be
local withoutCurrencySymbol = string.match(withCurrencySymbol,"%d+")
Edited by Freack100, 01 April 2015 - 04:27 PM.
#5
Posted 01 April 2015 - 09:55 PM
Indeed it is, but the point's that it's even simpler not to have it there in the first place.
#6
Posted 01 April 2015 - 10:22 PM
As Bomb Bloke stated, it's just a good practice to have the data in a consistant manner easily accessable. You don't want to spend half of your lines of code mangling around with formatting your data. You want to keep things simple, and by unwisely chosing default formats, you are adding complexity to your program that isn't necassary. Converting it one time isn't the problem, converting it everytime you need it however obscures the actual logic in your code, making it harder to read, and easier to break.
Edited by InDieTasten, 01 April 2015 - 10:23 PM.
3 user(s) are reading this topic
0 members, 3 guests, 0 anonymous users











