lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Saturday 08 January 2011 09:09:59 David Kastrup wrote:
> Alexander Gladysh <agladysh@gmail.com> writes:
> > On Sat, Jan 8, 2011 at 14:55, David Kastrup <dak@gnu.org> wrote:
> >> Alexander Gladysh <agladysh@gmail.com> writes:
> >>> On Sat, Jan 8, 2011 at 14:40, David Kastrup <dak@gnu.org> wrote:
> >>>> Alexander Gladysh <agladysh@gmail.com> writes:
> >>>>> On Sat, Jan 8, 2011 at 13:28, David Kastrup <dak@gnu.org> wrote:
> >>>>
> >>>> There is no way to insert nil in a table.  No matter what the user
> >>>> wants.
> >>>
> >>> Hmm. Why not?
> >>>
> >>> t = { 1, 2, 3, 4, 5 }
> >>> print("Before:", unpack(t, 1, 5))
> >>> for i = #t, 3, -1 do
> >>>   t[i + 1] = t[i]
> >>> end
> >>> t[3] = nil
> >>> print("After:", unpack(t, 1, 6))
> >>>
> >>> (Strictly speaking that is not a nil, but a "hole", but that is matter
> >>> of terminology.)
> >>
> >> No, it isn't.  If I now do t[4]=nil; t[5]=nil; t[6]=nil I have _not_
> >> touched t[3] at all, but clearly the table ends up with length 2.  So
> >> there definitely has not been a value at index 3.
> >
> > I am sorry, but I did not speak about "value". I did speak about nil
> > or a hole.
> 
> If you differentiate between the two, you clearly consider nil a value.
> 
> > I also haven't mentioned length of the table.
> 
> Just because you did not think through the consequences of your
> statement, it does not mean nobody else can.

Here's an obviously contrived program using nil insertions. My thought is if 
you can have a contrived example, there's probably a practical one somewhere 
out there. Therefore I'd think long and hard about considering nil insertion, 
at least with tab[x]=nil, to be a nop or impossible or even universally bad.

#!/usr/bin/lua

print("Strange IP address representation follows")
z={}
z[1] = 1
z[2] = 9
z[3] = 2
z[4] = nil
z[5] = 1
z[6] = 6
z[7] = 8
z[8] = nil
z[9] = 1
z[10] = nil
z[11] = 4
z[12] = 2
z[13] = nil
z[14] = nil

local ss = 1
while z[ss] ~= nil or z[ss+1] ~= nil do
	io.write(z[ss] or ".")
	ss = ss + 1
end
print()

========= Output follows ==============

slitt@mydesk:~$ ./test.lua
Strange IP address representation follows
192.168.1.42
slitt@mydesk:~$