Removing a number from a table that you don't know its position in the table

Say I have 3 numbers in a table and I want to remove one like following.  I want to remove the number 5 from the table so that 7 will be in the 1 spot and 10 in the 2 spot.  I cannot do ‘table.remove(ran, 1)’ because the 5 may not be in the 1 spot it will depend on the order in which the user presses the buttons.  The way I am doing it is not removing the ‘5’ from the table.  Thank you for assistance.

local ran = {}

table.insert(ran, 5)

table.insert(ran, 7)

table.insert(ran, 10)

table.remove(ran, ‘5’)

print( ran[2] )

–still printing 7, would like it to print 10

The problem is you should be doing

table.remove(ran, 2)

Removing the second position.

print( ran[2] ) will then print 10.

You will probably have to make your own function if you want to remove by value. 

Im pretty sure I  have to remove by value because there will be 4-10 values in the table that can be inserted and removed in any order dictated by what the user presses.  So if they press 8,24,67,5,33  then if they press 24 again it will be removed, leaving {8,67,5,33} in the table.  These numbers here will than be compared to 20 random numbers for matches, but that is later in the code.

Hopefully that will give you better vision to what I am trying to accomplish, if you have any insight into a better way to go about it please let me know.  Thank you.

Can give this a shot:

[lua]local ran = {}

table.insert(ran, 5)

table.insert(ran, 7)

table.insert(ran, 10)

function table.removeValue(t, value)

    local function findKey(t, value)

        for i = 1, #t do

            if t[i] == value then

                return i

            end

        end

    end 

    table.remove(ran, findKey(ran, 5))

end

table.removeValue(ran, 5)

print( ran[2] )

[/lua]

@Russm305

I think you going to complex

Do not put the selections into the table as the user picks /unpicks them - just have flags that go true / false per 80 numbers , combined with a count to make sure they cannot select more than allowed.

And then have a button which starts the random 20 selection BUT before that, have a function call to a function that loops through the flags and if true then put into the table.

Map out what will happen in the game and then what needs to happen and at what actual order - then you will be able to see where the best place is to put things to simplify your code and any possible shortcuts or better ways to achieve your result.

That would be my advice.

T.

Great advice ToeKnee thank you!

The problem is you should be doing

table.remove(ran, 2)

Removing the second position.

print( ran[2] ) will then print 10.

You will probably have to make your own function if you want to remove by value. 

Im pretty sure I  have to remove by value because there will be 4-10 values in the table that can be inserted and removed in any order dictated by what the user presses.  So if they press 8,24,67,5,33  then if they press 24 again it will be removed, leaving {8,67,5,33} in the table.  These numbers here will than be compared to 20 random numbers for matches, but that is later in the code.

Hopefully that will give you better vision to what I am trying to accomplish, if you have any insight into a better way to go about it please let me know.  Thank you.

Can give this a shot:

[lua]local ran = {}

table.insert(ran, 5)

table.insert(ran, 7)

table.insert(ran, 10)

function table.removeValue(t, value)

    local function findKey(t, value)

        for i = 1, #t do

            if t[i] == value then

                return i

            end

        end

    end 

    table.remove(ran, findKey(ran, 5))

end

table.removeValue(ran, 5)

print( ran[2] )

[/lua]

@Russm305

I think you going to complex

Do not put the selections into the table as the user picks /unpicks them - just have flags that go true / false per 80 numbers , combined with a count to make sure they cannot select more than allowed.

And then have a button which starts the random 20 selection BUT before that, have a function call to a function that loops through the flags and if true then put into the table.

Map out what will happen in the game and then what needs to happen and at what actual order - then you will be able to see where the best place is to put things to simplify your code and any possible shortcuts or better ways to achieve your result.

That would be my advice.

T.

Great advice ToeKnee thank you!