Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6917129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:41:28+00:00 2026-05-27T09:41:28+00:00

I just began my adventure with Lua and I’m hitting some issues. I’ve got

  • 0

I just began my adventure with Lua and I’m hitting some issues.

I’ve got a table, which holds instances of object that are use in my application.
I do some stuff with every entry and when I’m done I want to remove it from the table.

Let’s say the scenario is like this:

  • create 10 instances and insert them into the table

  • do some calculations on some of them [random choice]

  • once calculation is done, remove entry from the table

  • in the meantime add 10 more instances to the table

  • after 6th calculation remove all entries from the table

Since I know how many objects were added from the beginning I am using this count as key of the entry:

table.insert(myTable, tostring(myObject.objectNumber), myObject)

I am using tostring to make sure I don’t run into no key issues [for example, count starts with 0].

I wanted to remove entries using:

table.remove(myTable, tostring(myObject.objectNumber))

But it’s not the key that I have to pass as the second argument but position in the table. This screws the whole idea and I’m a bit lost on how to remove the entry properly without doing a loop over table each time. I cannot see any function which would give me table position of an entry by key.

EDIT:
So the problem is a little bit larger then I thought initially.
First of all:

table.insert(myTable, tostring(0), "something")
assert #myTable == 0

I can see that in my logs:

enemy count:    0
Inserting   0   table: 0x18e8a50
Insert check    0   table: 0x18e8a50
enemy count:    0
Inserting   1   table: 0x18c7c40
Insert check    1   table: 0x18c7c40
enemy count:    1

Also it is not returned by ipairs().

I’m not sure why, but it is how it is.

Second of all,

Inserting   0   table: 0x1781ac60
Insert check    0   table: 0x1781ac60
Inserting   1   table: 0x5e8c20      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Insert check    1   table: 0x5e8c20 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Inserting   2   table: 0x17807390
Insert check    2   table: 0x17807390
Inserting   3   table: 0x5f5a30
Insert check    3   table: 0x5f5a30
Inserting   4   table: 0x18c7850
Insert check    4   table: 0x18c7850
Inserting   5   table: 0x5e15f0
Insert check    5   table: 0x5e15f0
Inserting   6   table: 0x1784c540
Insert check    6   table: 0x1784c540
Inserting   7   table: 0x5a7b80
Insert check    7   table: 0x5a7b80
Inserting   8   table: 0x18f6d30
Insert check    8   table: 0x18f6d30
Inserting   9   table: 0x189d3e0
Insert check    9   table: 0x189d3e0
remove  0   table: 0x1781ac60
remove check    0   nil 9
Inserting   10  table: 0x18e9c50
Insert check    10  table: 0x18e9c50
Inserting   11  table: 0x5d64a0
Insert check    11  table: 0x5d64a0
Inserting   12  table: 0x19d43540
Insert check    12  table: 0x19d43540
Inserting   13  table: 0x18d5730
Insert check    13  table: 0x18d5730
Inserting   14  table: 0x19d19110
Insert check    14  table: 0x19d19110
Inserting   15  table: 0x595800
Insert check    15  table: 0x595800
Inserting   16  table: 0x5e0f30
Insert check    16  table: 0x5e0f30
remove  5   table: 0x5e15f0
remove check    5   nil 16
remove  4   table: 0x18c7850
remove check    4   nil 16
remove  3   table: 0x5f5a30
remove check    3   nil 16
remove  2   table: 0x17807390
remove check    2   nil 16
remove  6   table: 0x1784c540
remove check    6   nil 16
remove  7   table: 0x5a7b80
remove check    7   nil 16
Inserting   17  table: 0x56fcf0
Insert check    17  table: 0x56fcf0
remove  1   table: 0x5e8c20 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
remove check    1   nil 17 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
remove  8   table: 0x18f6d30
remove check    8   nil 17
Inserting   18  table: 0x5970a0
Insert check    18  table: 0x5970a0
remove  9   table: 0x189d3e0
remove check    9   nil 18
Removing all entries:   
1   table: 0x5e8c20     <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
2   table: 0x17807390
3   table: 0x5f5a30
4   table: 0x18c7850
5   table: 0x5e15f0
6   table: 0x1784c540
7   table: 0x5a7b80
8   table: 0x18f6d30
9   table: 0x189d3e0
10  table: 0x18e9c50
11  table: 0x5d64a0
12  table: 0x19d43540
13  table: 0x18d5730
14  table: 0x19d19110
15  table: 0x595800
16  table: 0x5e0f30
17  table: 0x56fcf0
18  table: 0x5970a0
remove  1   table: 0x5e8c20 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

As you can see although I have removed the entry earlier, it seems to still be there.
Now.. I thought it was a global-local variable issue with the table, but I’ve added the count of entries to the print [last entry in remove check lines].
The list after “Removing all entries” was created using for loop over pairs(myTable).

Here’s the code where I insert and remove entries:

MyObject = {}
MyObject_mt = { __index = MyObject }

function MyObject:new(params)
    MyObject = {
        objectNumber = params.objectNumber
    }
    local myObject = setmetatable(MyObject, MyObject_mt)

    print("Inserting", tostring(myObject.objectNumber), tostring(myObject))
    table.insert(myTable, tostring(myObject.objectNumber), myObject)
    print("Insert check", tostring(myObject.objectNumber), tostring(myObject))

    function MyObject:removeObject(event)
        print("remove", self.objectNumber, tostring(self))
        myTable[tostring(self.objectNumber)] = nil
        print("remove check", self.objectNumber, tostring(myTable[self.objectNumber]), #myTable)
        end
    end

    return myObject
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:41:29+00:00Added an answer on May 27, 2026 at 9:41 am

    Well assuming you dont have consecutive object numbers (i.e. don’t use the table as an array but rather as a dictionary) you can do simply a

    table[tostring(myObject.objectNumber)] = nil;
    

    Thats it, entry removed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey I just began working on a new project that requires, tab navigation, and
My boss has a C++ project that just began hanging today when he hits
I've just began using classes and would like to make some functions of a
I just began my adventure with java. I have written an application with a
I just began having problems with Indy. For some weird reason, even this very
Just began to develop using LINQ, and still can't understand some simple things. So,
I just began experimenting with FluentMigrator . I noticed that failed migrations are not
I just began using Visual Studio to work on a practice .NET project. I
I just began reading more about Markov Chain Generators today, and am really intrigued
I just began looking into source control.... And installed subversion from collabnet... I also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.