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 8771885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:54:09+00:00 2026-06-13T17:54:09+00:00

Say I have a table defined like this: myTable = { myValue = nil,

  • 0

Say I have a table defined like this:

myTable = { myValue = nil, myOtherValue = nil}

How would I iterate through it in a for each fashion loop like this?

  for key,value in myTable do --pseudocode
        value = "foobar"
  end

Also, if it helps, I don’t really care about the key, just the value.

  • 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-06-13T17:54:11+00:00Added an answer on June 13, 2026 at 5:54 pm

    Keys which have no value (ie: are nil) do not exist. myTable is an empty table as far as Lua is concerned.

    You can iterate through an empty table, but that’s not going to be useful.

    Furthermore:

    for key,value in myTable do --pseudocode
        value = "foobar"
    end
    

    This “pseudocode” makes no sense. You cannot modify a table by modifying the contents of a local variable; Lua doesn’t work that way. You cannot get a reference to a table entry; you can only get a value from the table.

    If you want to modify the contents of a table, you have to actually modify the table. For example:

    for key,value in pairs(myTable) do --actualcode
        myTable[key] = "foobar"
    end
    

    Notice the use of myTable. You can’t modify a table without using the table itself at some point. Whether it’s the table as accessed through myTable or via some other variable you store a reference to the table in.

    In general, modifying a table while it iterating through it can cause problems. However, Lua says:

    The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may clear existing fields.

    So it’s perfectly valid to modify the value of a field that already exists. And key obviously already exists in the table, so you can modify it. You can even set it to nil with no problems.

    Variables in Lua are nothing more than holders for values. Tables contain values; myTable[key] returns a value. You can store that value in a variable, but changing the variable will not change the value of myTable[key]. Since tables are stored by reference, you could change the contents of the table in one variable and see the changes in another, but that is simply the contents of the table, not the table itself.

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

Sidebar

Related Questions

Let's say I have a table called Customer, defined like this: Id Name DepartmentId
Say I have a table that goes clockwise like this: 2 1 3 0
I have a table with text in various language. Its defined like this: Id|Language|Text
I've noticed something odd about user-defined variables: Lets say I have this table: num_table
Say I have this table schema. ID AccNo Amount Say I have this data
Say I have a table: <table id=#myTable> <tr> Section 1 <td> <table> <tr> Sub
Say I have a table defined such that: Entities has_many Images Images has_one Entity
Let's say I have a table defined as follows: CREATE TABLE SomeTable ( P_Id
Let's say we have a simple table defined as: <p:dataTable value=#{testBean.dummyStringData} var=data> <p:column> <p:commandLink
I have defined my own function, like this: jQuery.fn.createSelector = function(){ var element =

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.