This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
GreenGene's profile picture

Get keys

Started by GreenGene, 01 April 2015 - 12:58 AM
GreenGene #1
Posted 01 April 2015 - 02:58 AM
I am makeing a program and i have local items = {
["dirt"] = { "$1" },
["cables"] = { "$10" }
}

How to get the keys dirt and cables etc?
Lupus590 #2
Posted 01 April 2015 - 03:14 AM
I believe it's pairs

for key,value in pairs(table) do
   print(key.." "..value)
end
InDieTasten #3
Posted 01 April 2015 - 04:39 AM
If Lopus590s answer doesn't help your problem, you may want to perform an index operation?:

print(items.dirt[1]) --#prints $1
print(items.cables[1]) --#prints $10
I 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 ;)/> Just be sure to never call your actual table you are working on "table" ;)/>
Edited on 01 April 2015 - 02:46 AM
H4X0RZ #4
Posted 01 April 2015 - 05:25 PM
If Lopus590s answer doesn't help your problem, you may want to perform an index operation?:

print(items.dirt[1]) --#prints $1
print(items.cables[1]) --#prints $10
I 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 ;)/> Just be sure to never call your actual table you are working on "table" ;)/>

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 on 01 April 2015 - 03:27 PM
Bomb Bloke #5
Posted 01 April 2015 - 10:55 PM
Indeed it is, but the point's that it's even simpler not to have it there in the first place.
InDieTasten #6
Posted 01 April 2015 - 11: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 on 01 April 2015 - 09:23 PM