[prev in list] [next in list] [prev in thread] [next in thread] 

List:       lua-l
Subject:    Re: Suspicious table initialization
From:       Grzegorz_Krasoń <grzegorz.krason () gmail ! com>
Date:       2020-10-27 20:20:40
Message-ID: CAFu0czA_HJgT+wTfSz0kDFJ-709gtPFVaQ9vNGYAuy31eJWkhQ () mail ! gmail ! com
[Download RAW message or body]

Thank you for the answers and references. It is clear now!

On Tue, 27 Oct 2020 at 20:53, Javier Guerra Giraldez <javier@guerrag.com>
wrote:

> On Tue, 27 Oct 2020 at 14:27, Grzegorz Krasoń <grzegorz.krason@gmail.com>
> wrote:
> >
> > How to understand the difference between t1 and t2 in the example below?
> They give the same results while iterating, but `#` returns different
> values.
> > Is this expected?
>
> yes
>
> > > t1 = {[3] = 100}
> > > t2 = {nil, nil, 100}
>
> these are two different tables with equal contents.  the difference is
> that they are independent objects.
>
> the `#` operator returns a number such that t[#t+1] == nil and either
> #t == 0 or t[#t] ~= nil.
>
> in both t1 and t2 it would be valid to return either 0 or 3.  which
> one is undefined.
>
>
> --
> Javier
>

[Attachment #3 (text/html)]

<div dir="ltr">Thank you for the answers and references. It is clear \
now!</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, 27 \
Oct 2020 at 20:53, Javier Guerra Giraldez &lt;<a \
href="mailto:javier@guerrag.com">javier@guerrag.com</a>&gt; \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Tue, 27 Oct 2020 at \
14:27, Grzegorz Krasoń &lt;<a href="mailto:grzegorz.krason@gmail.com" \
target="_blank">grzegorz.krason@gmail.com</a>&gt; wrote:<br> &gt;<br>
&gt; How to understand the difference between t1 and t2 in the example below? They \
give the same results while iterating, but `#` returns different values.<br> &gt; Is \
this expected?<br> <br>
yes<br>
<br>
&gt; &gt; t1 = {[3] = 100}<br>
&gt; &gt; t2 = {nil, nil, 100}<br>
<br>
these are two different tables with equal contents.   the difference is<br>
that they are independent objects.<br>
<br>
the `#` operator returns a number such that t[#t+1] == nil and either<br>
#t == 0 or t[#t] ~= nil.<br>
<br>
in both t1 and t2 it would be valid to return either 0 or 3.   which<br>
one is undefined.<br>
<br>
<br>
-- <br>
Javier<br>
</blockquote></div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic