1

I have two arrays:

A={4,8,12}
B={1,2,3,5,6,7,9,10,11}

And these code, the essence of which is, when calling the function, take a random value from array B, insert it in the order of the queue into array A, and, in another loop, remove that random value from array B.

local function Change ()
    local q= math.random(1, #B)

    for i = 1, #A do
        if B[q]<A[i] then
            for t=#A, i, -1 do
                A[t+1]=A[t]
            end
            A[i]=B[q]
            break
        end
    end

    while q<= #B-1 do
        B[q]=B[q+1]
        q=q+1
    end
    B[q]=nil

    for x = 1, #A do print ("Current: ", A[x]) end
    for y = 1, #B do print ("Free: ", B[y]) end
end

Now I want to use the same code, for the inverse problem - take a random value from array A, insert it in the order of the queue into array B, and then delete the value from array A.

So I want something like this:

Change (A, B) – find random value in B and insert to A (arrays)
Change (B, A) – find random value in A and insert to B (arrays)

Is there any way in Lua to do this? Pointers or so...

P.S. I dont want to use "table.sort/table.insert", etc for this task.

P.P.S. We found solution in thread with pointer, but today I found another solution:

C={{4,8,12},{1,2,3,5,6,7,9,10,11}}

Local function Change (x,y)
    a= math.random(1, #C[y])

    for i = 1, #C[x] do
        if C[y][a]<C[x][i] then
            for t=#C[x], i, -1 do
                C[x][t+1]=C[x][t]
            end
            C[x][i]=C[y][a]
            break
        end
    end

    while a<= #C[y]-1 do
        C[y][a]=C[y][a+1]
        a=a+1
    end
    C[y][a]=nil

    for i = 1, #C[x] do print ("in C"..x, C[x][i]) end
    for i = 1, #C[y] do print ("in C"..y, C[y][i]) end
end

and call Change(1,2) or Change (2,1)

How do you think, which method is better in performance?

Also I have another question in LUA syntax

When I make

C={ A={4,8,12}, B={1,2,3,5,6,7,9,10,11} }

And try inside the function #C[1] or #C[2] I got error «attempt to get length of field '?' (a nil value)» but it was very clear to write C.A or C.B instead of C[1], C[2] later in code

I had to initialize them like

A={4,8,12}, B={1,2,3,5,6,7,9,10,11}, C={A,B}

but got other error «attempt to get length of field ‘C’ (a nil value)» when use C.A or C.B.

Dead Krow
  • 11
  • 1
  • 2
  • Do you have something against `table.insert` ? It does the job, pretty much same way, but written in native code, using raw access methods. It will/should outperform your explicit Lua implementation. – Vlad Oct 19 '19 at 19:07
  • Every guide, which I had read, suggest not to use anything with table. because of productivity issues. Like this: https://stackoverflow.com/questions/12394841/safely-remove-items-from-an-array-table-while-iterating – Dead Krow Oct 20 '19 at 17:04
  • That link tells nothing about `table.insert`, showing an example of inefficient use of `table.remove`, when the results could be achieved faster in different way. If you measure the speed of `table.insert`, you'll find it twice faster than your plain Lua implementation. – Vlad Oct 20 '19 at 18:54
  • I'm using Corona SDK with lua 5.1 and official perfomance guide says Avoid "table.insert()" https://docs.coronalabs.com/guide/basics/optimization/index.html – Dead Krow Oct 20 '19 at 19:06
  • Again bad example. That article was about appending the elements. And it's not the `table.insert` that should be avoided, but the whole idea of inserting elements in a table with shifting all the rest of the data. But that's exactly what you do in your source. So if the algorithm complexity is the same, you should prefer native function `table.insert` over plain Lua implementation. It's faster and easier to read. – Vlad Oct 20 '19 at 19:31
  • In any case, I will have to sort the array, either preliminary, to immediately insert the element in the right place, or after the element is at the end of the array. So is it not better to do all this right away and break, than execute two commands throughout the array? – Dead Krow Oct 21 '19 at 04:04

1 Answers1

0

In Lua, tables are the only objects which are shared when placed in variables.

mytable = {1,2,3}
newtable = mytable

newtable[1] = "one"

print(mytable[1]) --> prints: "one"

So in your code, we can change the hardcoded tables to variables:

local function Change(insertTable, valueTable)
    local q = math.random(1, #valueTable)

    for i = 1, #insertTable do
        if valueTable[q] < insertTable[i] then
            for t = #insertTable, i, -1 do
                insertTable[t+1] = insertTable[t]
            end
            insertTable[i] = valueTable[q]
            break
        end
    end

    while q <= #valueTable-1 do
        valueTable[q] = valueTable[q+1]
        q = q+1
    end
    valueTable[q] = nil

    for x = 1, #insertTable do print("Current: ", insertTable[x]) end
    for y = 1, #valueTable do print("Free: ", valueTable[y]) end
end
anderium
  • 194
  • 5
  • 11
  • Thank You! Making this ``` A1 = {4,8,12} A = A1 B1 = {1, 2,3,5,6,7,9,10,11} B = B1 ``` And call like Change (A,B) or Change (B,A) help But I also found a solution - i update my post, so could you tell me, which method is better in perfomance – Dead Krow Oct 20 '19 at 17:48