Search code examples
lualua-table

How do you check row/Column wise iteration in Lua


I have been writing codes to perform loop in Column wise iteration and it makes me confuse all the time and I can't distinguish between Column and Row Wise Iteration.

I've written these code below to perform Column Wise Iteration but I'm not sure that it is performing in the right way. Also help me to understand with more examples of Row/Column Iteration

r = {}

for x = 1, 5 do
    table.insert(r, {})
end

for x = 1, 5 do
    for y = 1, 5 do
        table.insert(r[y], y)
        -- table.insert(r[x], 'A')
    end
end

for x = 1, 5 do
    for y = 1, 5 do
        io.write(r[x][y], ' : ')        
        -- io.write(r[y][x], ' :: ')
    end
    print()
end

OUTPUT:

1 : 1 : 1 : 1 : 1 : 
2 : 2 : 2 : 2 : 2 :
3 : 3 : 3 : 3 : 3 :
4 : 4 : 4 : 4 : 4 :
5 : 5 : 5 : 5 : 5 :

This one below is for Row Wise Iteration. I think!

L = {}

for i = 1, 5 do
    table.insert(L, {})
    for j = 1, 5 do
        L[i][j] = " # "
    end
end

for i = 1, 5 do
    for j = 1, 5 do
        io.write(L[i][j])
    end
    print()
end

OUTPUT:

 #  #  #  #  # 
 #  #  #  #  #
 #  #  #  #  #
 #  #  #  #  #
 #  #  #  #  # 

Solution

  • When more visualized and followed @LMD then you see more.
    I had some time so feel free to play around with...

    Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
    > L = require('rowcol')
    > L:ij()
        A   B   C   D   E
        F   G   H   I   J
        K   L   M   N   O
        P   Q   R   S   T
        U   V   W   X   Y
    > L:ji()
        A   F   K   P   U
        B   G   L   Q   V
        C   H   M   R   W
        D   I   N   S   X
        E   J   O   T   Y
    > io.open('rowcol.lua'):read('a')
    -- rowcol.lua
    
    local L = setmetatable({ -- Data Part
    [0x1] = {[0x1] = '\65', [0x2] = '\66', [0x3] = '\67', [0x4] = '\68', [0x5] = '\69'},
    [0x2] = {[0x1] = '\70', [0x2] = '\71', [0x3] = '\72', [0x4] = '\73', [0x5] = '\74'},
    [0x3] = {[0x1] = '\75', [0x2] = '\76', [0x3] = '\77', [0x4] = '\78', [0x5] = '\79'},
    [0x4] = {[0x1] = '\80', [0x2] = '\81', [0x3] = '\82', [0x4] = '\83', [0x5] = '\84'},
    [0x5] = {[0x1] = '\85', [0x2] = '\86', [0x3] = '\87', [0x4] = '\88', [0x5] = '\89'}
    },{__index = { -- Methods Part
    ij = function(self, i, ie, j, je)
    for i = i or 0x1, ie or 0x5 do
        for j = j or 0x1, je or 0x5 do
            io.write('\t', self[i][j])
        end
        print()
    end
    end,
    ji = function(self, i, ie, j, je)
    for i = i or 0x1, ie or 0x5 do
        for j = j or 0x1, je or 0x5 do
            io.write('\t', self[j][i])
        end
        print()
    end
    end
    }
    })
    
    return L
    

    Both methods can handle both for range parameter...

    > L:ji(5,5)
        E   J   O   T   Y
    > L:ij(5,5)
        U   V   W   X   Y
    > L:ij(5,5,5,5)
        Y
    > L:ij(3,5,3,5)
        M   N   O
        R   S   T
        W   X   Y
    > L:ji(3,5,3,5)
        M   R   W
        N   S   X
        O   T   Y
    > L:ji(1,5,1,5)
        A   F   K   P   U
        B   G   L   Q   V
        C   H   M   R   W
        D   I   N   S   X
        E   J   O   T   Y
    > L:ji(1,5,1,4)
        A   F   K   P
        B   G   L   Q
        C   H   M   R
        D   I   N   S
        E   J   O   T
    > L:ji(1,5,1,3)
        A   F   K
        B   G   L
        C   H   M
        D   I   N
        E   J   O