Log In  

Hello, hopefully I've posted this in the correct area, the BBS is a little confusing.

The three lines below are really troubling me.
The first one demonstrates that we have access to i, x, and y.
The second one demonstrates that we have access to a[1][1][1].
The third one should evaluate to a[1][1][1] which we have access to and stores the value "0", but instead it crashes.

The code:

print(i..x..y) -- returns "111"
print(a[1][1][1]) -- returns "0" which is the correct value stored there
print(a[i][x][y]) -- error

The error:

print(a[i][x][y]) -- error
attempt to index field "?" (a nil value)

Does anyone know why it works this way? Any advice on the correct syntax? I can post the full code if needed, but it's somewhat obfuscated in favor of fewer characters.

Thanks for reading!

P#90827 2021-04-20 01:44

Are i, x, y strings or numbers?

E: Also, are those three lines in the same scope?

P#90831 2021-04-20 03:09 ( Edited 2021-04-20 04:02)

Can't reproduce that:

i,x,y=1,1,1
a={}
a[1] = {}
a[1][1] = {}
a[1][1][1] = 0

print(a[1][1][1]) -- prints "0"
print(a[i][x][y]) -- prints "0"
P#90832 2021-04-20 03:55

Thanks for the replies, both of you. I think I figured it out based on what cakepie said. Looking at my code, I'm using sub() to load "x" and "y", drawing from a string, so even though I'm drawing numbers, they're probably being handled as a string. I'll try a different implementation of that part, and I'll post back here if I either have any more trouble or land on a specific implementation that's simple and relevant enough to share. Might be as simple as removing quotations or casting to another type, I'm fairly new to Lua obviously, but I'd imagine I can figure out something.

Here's the full code for anyone curious, sorry for the delay. I'm sure it looks really weird, haha. I'm generating an array to store tetrominoes, which I'll reference later. The strings contain information about which spaces need to be filled for each of the tetrominoes, with every 4 characters in "s" describing the 4 places blocks can go in a shape, and every two characters in "c" acting as coordinates describing where to place each numerical block in a 2d array, which itself is stored in an array, indexing each possible shape a tetromino can be.

The print() calls in the OP were just for debugging purposes.

s="1358468935895689678956795690"
c="11211222132333142434"
a={}
for i=1,7 do
    a[i]={}
    for j=1,3 do
        a[i][j]={}
        for k=1,4 do
            a[i][j][k]=0
        end
    end
    for l=1,28 do
        d=sub(s,l,l)
        x=sub(c,d,d)
        y=sub(c,d+1,d+1)
        print(i..x..y) -- "111"
        print(a[1][1][1]) -- "0"
        print(a[i][x][y]) -- error
        a[i][x][y]=1
        d+=2
    end
end
P#90875 2021-04-21 05:23 ( Edited 2021-04-21 05:38)

Correct, 'sub' returns a substring from a string, you will have to call 'tonum' yourself if you want to transform to numbers (or call 'tostr' if you have a number and want to index your table that uses string keys).

P#90888 2021-04-21 14:21

Thanks for that, even saved me the trouble of scouring reference material looking for the correct functions. We'll call this one solved, much appreciated all!

P#90889 2021-04-21 15:28

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-20 02:41:13 | 0.006s | Q:14