lua-users home
lua-l archive

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


On Tuesday 28 December 2010 15:02:21 Steve Litt wrote:
> On Tuesday 28 December 2010 07:11:30 Dirk Laurie wrote:
> > On Tue, Dec 28, 2010 at 01:17:31PM +0200, Axel Kittenberger wrote:
> > > Would'nt it sufficide in the first part to increase just an integer
> > > instead? ---
> > > local n = 0
> > > for i, _ in ipairs(t) do
> > >     n = i
> > > end
> > >
> > > for k, v in pairs(t) do
> > >    if type(k) ~= "number" or k<1 or k>n then
> > >       --- someting
> > >   end
> > > end
> >
> > No.
> >
> > t={1,2}; t[1.5]=1.5
> 
> Dirk,
> 
> I think this works to deliver everything except what's delivered by
>  ipairs():
> 
> #!/usr/bin/lua
> 
> local arr={"one", "two", "three"}
> 
> arr[4] = "four"
> arr[1.5] = "one point five"
> arr["steve"] = "Litt"
> 
> for k, v in pairs(arr) do
> 	local skip = true
> 	if type(k) == "number" then
> 		local w, f = math.modf(k)
> 		if f ~= 0 then skip = false end
> 	else
> 		skip = false
> 	end
> 	if not skip then
> 		print(k)
> 		print(v)
> 		print("")
> 	end
> end
> 
> I'm sure the preceding can be cleaned up a lot. Obviously, I wrote the
> preceding with a C/Perl accent, but you get the idea.

I think I simplified it:

#!/usr/bin/lua 

local arr={"one", "two", "three"}

arr[4] = "four"
arr[1.5] = "one point five"
arr["steve"] = "Litt"

for k, v in pairs(arr) do
	if type(k) ~= "number" or math.ceil(k) ~= k then
		print(k)
		print(v)
		print("")
	end
end




Steve Litt
Recession Relief Package
http://www.recession-relief.US
Twitter: http://www.twitter.com/stevelitt