lua-users home
lua-l archive

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


It was thus said that the Great Rodrigo Azevedo once stated:
> 
> Let's try an even simpler model: (alternative to a specific table
> constructor)
> 
> Definitions: t is a table
> 
> 0) a 'sequence' is a continuous set of integer keys with non-nil values
> 1) #t (rawlen) operator: biggest non-zero positive integer key of the
> sequence starting from key 1 [1]
> 2) t# (rawborder) operator: biggest non-zero positive integer key assigned
> (rawset)

  t    = {}
  t[3] = 3 
  t[1] = 1
  t[5] = 5
  t[2] = 2
  t[4] = 4

  	#t == 5 -- because this is a sequence
  	t# == 5 -- because 5 is the largest non-zero positive integer key

  table.remove(t)

  	#t   == 4 -- because this is still a sequence
  	t#   == 4 -- beause we removed 5
  	t[4] == 4 -- because we removed the last element

  table.remove(t,1)

  	#t   == 3 -- beacuse this is still a sequence
  	t#   == 4 -- because 4 is still the largest non-zero positive integer key
  	t[3] == 4 -- because we removed the first element

  t[500] == 500

  	#t == 3   -- because this is still a sequence
  	t# == 500 -- because 500 is still the largest non-zero positive integer key

  table.remove(t,2)

  	#t     == 2   -- because this is still a sequence
  	t#     == 500 -- because 500 is still the largest non-zero positive integer key
	t[2]   == 4   -- because we removed an element
	t[500] == 500 -- because this isn't part of the sequence

> Examples:
> 
> t = {1,2,3,4,5,nil,nil,8,nil} -- two sequences
> #t is 5
> t# is 9

	table.remove(t)

  Which element was removed?  Where will you find '8'?  Is this an issue?

  -spc