Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes it is 4.</p> <p>From the Lua reference manual:</p> <blockquote> <p>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).</p> </blockquote> <p>Let's modify the code to see what is in the table:</p> <pre><code>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) </code></pre> <p>When you run this you see that <code>objects[4]</code> is 3 but <code>objects[5]</code> is <code>nil</code>. Here is the output:</p> <pre><code>$ 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 </code></pre> <p>It is true that you filled in 15 slots of the table. However the <code>#</code> 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 <strong>is</strong> nil.</p> <p>In this case, the index that satisfies this condition is 4.</p> <p>That is why the answer is 4. It's just the way Lua is.</p> <p>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 <em>the end of a string</em> and the "string" is only those characters before it.</p> <p>If your intent was to produce the table <code>1,1,1,2,2,2,3,3,3,4,4,4,5,5,5</code> then you will need to rewrite your <code>quantize</code> function as follows:</p> <pre><code>function quantizeNumber(i, step) return math.ceil(i / step) end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload