[Home] [Downloads] [Search] [Help/forum]

Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  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] Refresh page

Posted by Faux   United Kingdom  (77 posts)  [Biography] bio
Date Mon 17 Jan 2005 02:54 AM (UTC)

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

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)
			Note ("[" .. tostring (key) .. "] = " .. tostring (value))

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

function explode(d,p)
	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.
			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.
	return t

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

function implode(d,t)
	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

..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
	return p

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

[Go to top] 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.


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

Go to topic:           Search the forum

[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]