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

Unpack

Started by Rougeminner, 25 January 2015 - 03:31 PM
Rougeminner #1
Posted 25 January 2015 - 04:31 PM
I need some help with unpack. my goal is to get a specific table element. once i find that element i want to get the values associated to it in the table. my problem lies on the unpack statement i want the line print("It should print right here",unpack(vectors[detail])) here is a link to pastern code http://www.pastebin.com/twM2Y0iU Please help me. i am going crazy trying to figure this out.
InDieTasten #2
Posted 25 January 2015 - 04:46 PM
In your pastebin you are "calling" unpack with

unpack { vectors[detail] }
I'm pretty sure you want to do this:

unpack( vectors[detail] )
because what you are doing is putting your table into another table, and unpack one layer, which is as if you would write:

vectors[detail]
which will be your table/3d-vector containing you 3 dimension values parsed before

So your code formatted properly:
Spoiler

  

vectors = { }
function printUsage()
    print("Uses are gps+ set <name of waypoint>")
    print("or you can do gps+ go <waypoint>")
end
     
local tArgs = { ...}
     
if #tArgs < 1 then
    printUsage()
    return
end
local sCommand = tArgs[1]
local detail = tArgs[2]
if sCommand == "set" then
    x1, x2, x3 = gps.locate()
    print(gps.locate(9))
    print("gps Cords,", x1, ',', x2, ',', x3)
    x11 = toFloat(x1)
    x22 = toFloat(x2)
    x33 = toFloat(x3)
    -- Above is to float below is tostring
    x1 = tostring(x1)
    x2 = tostring(x2)
    x3 = tostring(x3)
    -- x4 = x1....x2....x3

    -- create addin code
    print(gps.locate())
    loc, loc1, loc2 = gps.locate()
    -- lo1 = --loc..loc2--,loc2
    -- print(loc,',',loc1)--,',loc2)
    strg = detail .. " = " .. loc .. ',' .. loc1 .. ',' .. loc2
    -- print(strg)
    h = fs.open("apps/gps+/vectorfile", "a")
    h.writeLine(strg)
    h.close()
    -- print(detail)
    -- print(test)

elseif sCommand == "go" then
    print(detail)
    h = fs.open('apps/gps+/vectorfile', 'r')
    -- f1 = h.readAll()
    f2 = string.find(h.readAll(), detail)
    if f2 then
	    -- vectorfile = os.loadAPI("apps/gps+/vectorfile")
	    -- print(vectorfile.test())
	    print("returned true")
	    print(detail)
	    file = fs.open("apps/gps+/vectorfile", 'r')
	    for line in file.readAll():gmatch("[^\n]+") do
		    vectors[line:match("[^=%s]+")] = ( { line:match("(%d+),(%d+),(%d+)") })
	    end
	    file.close()
	    -- print(table.foreach(vectors[detail],print))
	    print("here is where it is supposed to be ", unpack ( vectors[detail] ))
    else
	    print("couldn't find waypoint. Does it exist?")
    end
elseif sCommand == "flush" then
    h = fs.open("apps/gps+/vectorfile", 'w')
    h.flush()
    h.writeLine("-- Here is where gps+ stores your waypoints cords.")
    h.close()
    print("flush complete")
else
    printUsage()
end
     
     
     
     
     
     
-- home = vector.new(-5321,76,-1383)
-- pos = gps.locate(2)
-- print("Your current Position is ",home)
 
InDieTasten #3
Posted 25 January 2015 - 05:43 PM
Another way to do it would be the following:

vector[detail] = {15, 12, 7}

print("your string", vector[detail][1], vector[detail][2], vector[detail][3])