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

displaying integers in a table

Started by shadowreaper5, 19 May 2015 - 07:45 PM
shadowreaper5 #1
Posted 19 May 2015 - 09:45 PM
Hey Pro,

I need a boat load of help. I am trying to make a table of aspects from the Aspectalyzer from Thaumic Tinkerer. Here's my code.
http://pastebin.com/z8Tr1DNy

When I run it on the monitor I get a string of numbers and letters instead of just a single number.

Here's what it looks like.
http://imgur.com/P6ZOQAm

I think it has something to do with the "tostring" command and something in the way I wrote the actual table but I am not sure.
Cranium #2
Posted 19 May 2015 - 10:04 PM
Each table created in Lua is assigned a memory reference ID. That's what you're printing there. You'll likely need to delve into the table being returned to print the info you need.
Dragon53535 #3
Posted 19 May 2015 - 10:22 PM
To find where the amount is stored, run this code for just a single jar.


for a,v in pairs(tbl) do
  print(a.."  :  "..tostring(v))
end
replace tbl with the variable for the jar's table.
shadowreaper5 #4
Posted 20 May 2015 - 06:06 AM
um, I appreciate the help, but maybe you could dumb it down even further for me. To do even basic coding I need a flow chart, you guys are talking up waaay above me. all I want is to take the aspect numbers from the GetAspectNumber command (which is an integer) and display them in a nice graphic that myself and the rest of my non-coder friends can look at on the monitor.
shadowreaper5 #5
Posted 20 May 2015 - 06:08 AM
To find where the amount is stored, run this code for just a single jar.


for a,v in pairs(tbl) do
  print(a.."  :  "..tostring(v))
end
replace tbl with the variable for the jar's table.
jar? there's no jar of aspects. I am using commands from this:
https://github.com/Thaumic-Tinkerer/ThaumicTinkerer/wiki/Peripheral-Documentation#aspectalyzer-commands
Bomb Bloke #6
Posted 20 May 2015 - 06:26 AM
Well, you know how you've got a table called "aspects", and you've got values in the table indexed against names like 'aer', 'aqua', 'ignis' and so on? Well, aspectalyzer.getAspectCount('whatever') is also returning tables, with values inside of them (which is odd, because the documentation you've linked says it should be returning numbers, but hey, that's what it's doing!). You can't convert the whole table to a string (well, not in any useful fashion; not here at least!), so you've got to figure out which value in the table you want, and grab that.

Try making a new test script and running this:

aspectalyzer = peripheral.wrap("left")
for a,v in pairs(aspectalyzer.getAspectCount('aqua')) do
  print(a.."  :  "..tostring(v))
end

This will give you a list of all the values in the table aspectalyzer.getAspectCount('aqua') returns, where "a" is the index name and "v" is the assigned value. Once you know the index names of interest, you should be able to make use of the values - just as you know your "aspects" table ends up with an index called "aqua", for example…

If you're not sure what to do with the results, then post them here and we can give you more examples. Certainly it'd be worthwhile taking a read through this in the meantime.
Edited on 20 May 2015 - 04:30 AM
shadowreaper5 #7
Posted 22 May 2015 - 10:08 PM
Well, you know how you've got a table called "aspects", and you've got values in the table indexed against names like 'aer', 'aqua', 'ignis' and so on? Well, aspectalyzer.getAspectCount('whatever') is also returning tables, with values inside of them (which is odd, because the documentation you've linked says it should be returning numbers, but hey, that's what it's doing!). You can't convert the whole table to a string (well, not in any useful fashion; not here at least!), so you've got to figure out which value in the table you want, and grab that. Try making a new test script and running this:
aspectalyzer = peripheral.wrap("left") for a,v in pairs(aspectalyzer.getAspectCount('aqua')) do print(a.." : "..tostring(v)) end
This will give you a list of all the values in the table aspectalyzer.getAspectCount('aqua') returns, where "a" is the index name and "v" is the assigned value. Once you know the index names of interest, you should be able to make use of the values - just as you know your "aspects" table ends up with an index called "aqua", for example… If you're not sure what to do with the results, then post them here and we can give you more examples. Certainly it'd be worthwhile taking a read through this in the meantime.

how should I go about figuring out what "a" and "v" are? Or should I just put "a" and "v".
Lupus590 #8
Posted 22 May 2015 - 11:06 PM
a is the key in the table (the one that was passed to pairs)
v is the value in that key (i.e. table[a] has the value of v)
valithor #9
Posted 22 May 2015 - 11:14 PM
snip

how should I go about figuring out what "a" and "v" are? Or should I just put "a" and "v".

I will show a short example of a table to help explain what a and v are in his example.


myTable = {
  ["variable1"] = "value1",
  ["variable2"] = "value2",
  ["variable3"] = "value3"
}
for k,v in pairs(myTable) do --# key and value are explained below - the 2 variables "k" and "v" can be anything
  print("The key is "..k) --# I could make the two variable apple and banana if I really wanted to
  print("The value is "..v) --# I prefer k and v just because they stand for key and value which makes it easier to read
end

The above is a very basic visual of a table. Now each time that for loop runs it will iterate through each variable and value. So the first time it runs "a" will be equal to "variable1" and "v" will be equal to "value1". The second time "a" will be equal to "variable2" and v will be equal to "value2" and so on until there is no entries in the table left.

All tables have entries like this, references (better known as keys) in this example the "variables", and the values in this example the "values".

So to explain what is going on in the code bomb bloke posted in the order the code is run is:
- We wrap the peripheral
- We run aspectalyzer.getAspectCount, which returns a table
- We pass that table to the function pairs, which iterates through each reference and value in the table and sets them to the two variables before the "in pairs" in the for loop
- We use the a and v variable in a print statement (a is the key, v is the value for that iteration)

Long story short… just put a and v
Edited on 22 May 2015 - 09:19 PM