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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T00:56:57+00:00 2026-05-25T00:56:57+00:00

Okay, so I’ve got a strange problem with the following Lua code: function quantizeNumber(i,

  • 0

Okay, so I’ve got a strange problem with the following Lua code:

function quantizeNumber(i, step)
    local d = i / step
    d = round(d, 0)
    return d*step
end

bar = {1, 2, 3, 4, 5}

local objects = {}
local foo = #bar * 3
for i=1, #foo do
    objects[i] = bar[quantizeNumber(i, 3)]
end
print(#fontObjects)

After this code has been run, the length of objects should be 15, right? But no, it’s 4. How is this working and what am I missing?

Thanks, Elliot Bonneville.

  • 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-25T00:56:59+00:00Added an answer on May 25, 2026 at 12:56 am

    Yes it is 4.

    From the Lua reference manual:

    The length of a table t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n can be zero. For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value. If the array has “holes” (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).

    Let’s modify the code to see what is in the table:

    local objects = {}
    local foo = #bar * 3
    for i=1, foo do
        objects[i] = bar[quantizeNumber(i, 3)]
        print("At " .. i .. " the value is " .. (objects[i] and objects[i] or "nil"))
    end
    print(objects)
    print(#objects)
    

    When you run this you see that objects[4] is 3 but objects[5] is nil. Here is the output:

    $ lua quantize.lua 
    At 1 the value is nil
    At 2 the value is 3
    At 3 the value is 3
    At 4 the value is 3
    At 5 the value is nil
    At 6 the value is nil
    At 7 the value is nil
    At 8 the value is nil
    At 9 the value is nil
    At 10 the value is nil
    At 11 the value is nil
    At 12 the value is nil
    At 13 the value is nil
    At 14 the value is nil
    At 15 the value is nil
    table: 0x1001065f0
    4
    

    It is true that you filled in 15 slots of the table. However the # operator on tables, as defined by the reference manual, does not care about this. It simply looks for an index where the value is not nil, and whose following index is nil.

    In this case, the index that satisfies this condition is 4.

    That is why the answer is 4. It’s just the way Lua is.

    The nil can be seen as representing the end of an array. It’s kind of like in C how a zero byte in the middle of a character array is actually the end of a string and the “string” is only those characters before it.

    If your intent was to produce the table 1,1,1,2,2,2,3,3,3,4,4,4,5,5,5 then you will need to rewrite your quantize function as follows:

    function quantizeNumber(i, step)
        return math.ceil(i / step)
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, I have the following PHP code to extract an email address of the
Okay, so I've got some C code to perform a mathematical operation which could,
Okay, so I'm running a small test webserver on my private network. I've got
Okay, we know that the following two lines are equivalent - (0 == i)
Okay, here's my current code for an array. $sites[0]['url'] = http://example0.com; $sites[1]['url'] = http://example1.com;
Okay. So I've got a a little jQuery gallery scroller I wrote to work
Okay, in python one can do this: def foo(monkeys): def bar(monkey): #process and return
okay code: #!/usr/bin/python import wx import sys class XPinst(wx.App): def __init__(self, redirect=False, filename=None): wx.App.__init__(self,
Okay. I have this code on my site: <?php session_start(); include database.php; include bruger.php;
Okay, so I have a snippet of code from a website containing a button:

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.