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

Gps Goto program. Question [Solved]

Started by Goof, 05 February 2013 - 08:37 AM
Goof #1
Posted 05 February 2013 - 09:37 AM
Hi

Can you guys please tell me how to get the more "advanced" table functions, such as




local tVar = {["Name"] = {...}} etc...





is this code actual functioning? if yes, then how could i say that it would take the "["Home"][1]" and then know the "wX = 0" should be 0, then just set the wX variable to 0. ? (do you understand what i mean?)

-- Waypoints --

local Waypoint = {
["Home"] = {wX = 0, wY = 70, wZ = 0, wF = 2} -- i dont know how to actually get TO the table. (i think its something like "Waypoint["Home"][1]" but i am not sure.
}
Can anyone tell me the explanations about the more "Advanced" tables ?


Thanks in advance :D/>
GopherAtl #2
Posted 05 February 2013 - 09:41 AM
using your waypoint example, "waypoint["Home"]["wX"]" would work, as would "waypoint.Home.wX"

"a.b" in lua is "syntactic sugar" for "a["b"]", so "." only works where b is a string, and not a lua keyword like "for" or "end"
Goof #3
Posted 05 February 2013 - 09:43 AM
so i just have to (the example)
do:


local Waypoint = {
["Home"] = {wX = 0, wY = 70, wZ = 0, wF = 2}
}

-- Call
xPos = Waypoint.Home.wX 
--
?:
GopherAtl #4
Posted 05 February 2013 - 09:45 AM
yawp
Goof #5
Posted 05 February 2013 - 09:46 AM
Thanks :D/>

edit:

is it possible to make more than 1 table in a table, like this:



local Waypoint = {
["Home"] = {
["sPos"] = {wX = 0, wY = 70, wZ = 0, wF = 2},
["ePos"] = {wX = 0, wY = 70, wZ = 0, wF = 2}
}
}

-- Call
xPos = Waypoint.Home.sPos.wX
--



Edit2: Figured out.. (Yes you can)
Edited on 05 February 2013 - 09:00 AM
Goof #6
Posted 05 February 2013 - 09:57 AM
Edit **^^**
GopherAtl #7
Posted 05 February 2013 - 09:58 AM
yawp
Goof #8
Posted 05 February 2013 - 10:14 AM
ehmm… i ran into a third question:

how do i then do a for i = 1, #waypoint?

What does it return, and what can i then do..


for k,v in ipairs(Waypoints) do -- do i need this ?

or:

for i = 1, #Waypoint do
goto(i) ???
end

Thanks.. i hope wouldnt waste your time.
GopherAtl #9
Posted 05 February 2013 - 11:06 AM
just use pairs instead of ipairs. ipairs is for arrays, with numeric indices starting with 1. pairs works for all keys.
Goof #10
Posted 05 February 2013 - 11:13 AM
So that would mean that the k is the variable for the name of the tables choice?
do you understand what i mean?

(will go to bed now. Will answer tomorrow…)
Lyqyd #11
Posted 05 February 2013 - 11:20 AM
In that case, k is the key. In this example:


myTable = {
  bob = true,
  [123] = "george",
  ["sam"] = 456
}

for k, v in pairs(myTable) do
  print(k)
end

…the output would be:

bob
123
sam

If we changed it to print v instead:

true
george
456

It doesn't matter what the values themselves are (v can easily be a number, a table, a function, whatever), the key will be in the first variable and the value in the second when using pairs().
Goof #12
Posted 06 February 2013 - 03:19 AM
*Ok THANKS!

You both are so helpfull! its really appreciated!

ps: would this code work?

when i tested it i actually got this error: gps:115: attempt to index ? (a nil value)


(pastebin (easier to read):
http://pastebin.com/0QMSdZGZ



(i know i shouldnt ask you to correct all my code, but i would not try the code if you mean my code is totally wrong)

Thanks for a good work!
Lyqyd #13
Posted 06 February 2013 - 06:48 AM
You'd need to use Waypoint[k].sPos.wX instead, since the index isn't at the string literal "k", it is at whatever value k holds. Also, that logic isn't what you want it to do, it needs to be more like:


if #args == 3 then
  goto(tonumber(args[1]), tonumber(args[2]), tonumber(args[3]))
elseif #args == 1 and Waypoints[args[1]] then --this essentially checks that the args[1] key in the Waypoints table is not nil or false.
  local dest = Waypoints[args[1]].sPos --then we use a reference to its subtable to keep the variable names short in the next line.
  goto(dest.wX, dest.wY, dest.wZ)
else
  printUsage()
end

This does happen to remove the loop (which isn't necessary if you are just checking that an index is valid), but it is definitely the better/simpler way of doing it.

Also, you need to catch the arguments at the beginning of your code:

local args = {…}

Otherwise, you'll have a hell of a time using them.
Goof #14
Posted 06 February 2013 - 07:04 AM
Thank you :D/>

i would take some more time on this, now :D/>



ehm that local args = {…} is actually in the start of the code, ( forgot to say that. xD )