Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Lua ➜ Emulation of some PHP functions

Emulation of some PHP functions

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Faux   United Kingdom  (77 posts)  Bio
Date Mon 17 Jan 2005 02:54 AM (UTC)

Amended on Mon 17 Jan 2005 08:40 PM (UTC) by Faux

Message
There's some functions that I can't cope without having, and.. as lua doens't provide them, I'm going to write them.

Hope someone else finds at least some of them useful :)

Starting with: print_r (based on Nick's code):
ie. print_r({5,3,{5,3}})

function print_r (t, indent, done)
	done = done or {}
	indent = indent or 0
	for key, value in pairs (t) do
		Tell (string.rep (" ", indent)) -- indent it
		if type (value) == "table" and not done [value] then
			done [value] = true
			Note ("[" .. tostring (key) .. "]:");
			print_r (value, indent + 4, done)
		else
			Note ("[" .. tostring (key) .. "] = " .. tostring (value))
		end
	end
end

and explode:
ie. explode(" ","this is a test string")

function explode(d,p)
	t={}
	ll=0
	while true do
		l=string.find(p,d,ll+1,true) -- find the next d in the string
		if l~=nil then -- if "not not" found then..
			table.insert(t, string.sub(p,ll,l-1)) -- Save it in our array.
			ll=l+1 -- save just after where we found it for searching next time.
		else
			table.insert(t, string.sub(p,ll)) -- Save what's left in our array.
			break -- Break at end, as it should be, according to the lua manual.
		end
	end
	return t
end


and implode:
ie. implode(" ",{"this","is","a","test","array"})

function implode(d,t)
	ss=""
	for a, v in pairs(t) do ss = ss .. v .. d end  -- foreach value, add it to ss and add the delimiter
	ss=string.sub(ss,0,string.len(ss)-string.len(d)) -- Remove the bogus ending delimiter.s
	return ss
end


..and preg_replace:
ie. preg_replace("\((.*?)\)","", " Obvious exits: n(closed) w(open) rift")
NOTE: Normal preg_replace supports using the wildcards in the replace, this doesn't.. yet

function preg_replace(pat,with,p)
	while true do
		a=rex.new(pat) -- compile regexp
		x,y,t=a:match(p) -- match regexp
		if (x~=nil and y~=nil) then -- if we matched then
			p=string.sub(p,0,x-1).. with .. string.sub(p,y+1) -- rebuild the string
		else
			break
		end
	end
	return p
end

Faux, from Discworld. Feel free to come talk to me =)

http://faux.servebeer.com/
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


9,350 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.