Jump to content




displaying integers in a table

help lua

  • You cannot reply to this topic
8 replies to this topic

#1 shadowreaper5

  • Members
  • 4 posts

Posted 19 May 2015 - 08: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.

#2 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 19 May 2015 - 09: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.

#3 Dragon53535

  • Members
  • 973 posts
  • LocationIn the Matrix

Posted 19 May 2015 - 09: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.

#4 shadowreaper5

  • Members
  • 4 posts

Posted 20 May 2015 - 05: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.

#5 shadowreaper5

  • Members
  • 4 posts

Posted 20 May 2015 - 05:08 AM

View PostDragon53535, on 19 May 2015 - 09:22 PM, said:

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/T...alyzer-commands

#6 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 20 May 2015 - 05: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 by Bomb Bloke, 20 May 2015 - 05:30 AM.


#7 shadowreaper5

  • Members
  • 4 posts

Posted 22 May 2015 - 09:08 PM

View PostBomb Bloke, on 20 May 2015 - 05:26 AM, said:

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".

#8 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 22 May 2015 - 10: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)

#9 valithor

  • Members
  • 1,053 posts

Posted 22 May 2015 - 10:14 PM

View Postshadowreaper5, on 22 May 2015 - 09:08 PM, said:

View PostBomb Bloke, on 20 May 2015 - 05:26 AM, said:

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 by valithor, 22 May 2015 - 10:19 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users