lua-users home
lua-l archive

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


Hi all,

Coming from the Perl world, I found myself wanting the equivalent of Perl's 
split() function, so I made it. Also wanting a title case converter, I created 
that. The third thing in the file I'm attaching is called titleCase2(), which 
I copied from the Internet, verified that it works, and haven't a clue why.

Anyway, check it out, and if any of you have been missing something like 
Perl's split(), here it is.

Thanks

SteveT

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


#!/usr/bin/lua

local titleCase = function(str)
	str = string.upper(string.sub(str,1,1)) .. string.sub(str,2)
	str = string.gsub(str, "(%s)(%S)", 
		function(spc, ltr)
			return(spc .. string.upper(ltr))
		end
	)
	return(str)
end

local titleCase2 = function(str)
	local function tchelper(first, rest)
		return first:upper()..rest:lower()
	end
	-- Add extra characters to the pattern if you need to. _ and ' are
	--  found in the middle of identifiers and English words.
	-- We must also put %w_' into [%w_'] to make it handle normal stuff
	-- and extra stuff the same.
	-- This also turns hex numbers into, eg. 0Xa7d4
	str = str:gsub("(%a)([%w_']*)", tchelper)
	return str
end

local split = function(regex, str)
	local tab = {}
	local startt = 1
	local s1, e1 = string.find(str, regex, startt)
	while s1 do
		table.insert(tab, string.sub(str, startt, s1-1))
		startt = e1 + 1
		s1, e1 = string.find(str, regex, startt)
	end
	table.insert(tab, string.sub(str, startt, -1))
	return(tab)
end


local mystring = "Carolyn Carson      cares	about cars!"

print(titleCase(mystring))

local tab = split("%s+", mystring)

for k, v in ipairs(tab) do
	print(tostring(k) .. ":" .. tostring(v))
end
print()

tab = split("[Cc]ar", mystring)

for k, v in ipairs(tab) do
	print(tostring(k) .. ":" .. tostring(v))
end
print()

tab = split("o", mystring)

for k, v in ipairs(tab) do
	print(tostring(k) .. ":" .. tostring(v))
end


#!/usr/bin/lua

local titleCase = function(str)
	str = string.upper(string.sub(str,1,1)) .. string.sub(str,2)
	str = string.gsub(str, "(%s)(%S)", 
		function(spc, ltr)
			return(spc .. string.upper(ltr))
		end
	)
	return(str)
end

local titleCase2 = function(str)
	local function tchelper(first, rest)
		return first:upper()..rest:lower()
	end
	-- Add extra characters to the pattern if you need to. _ and ' are
	--  found in the middle of identifiers and English words.
	-- We must also put %w_' into [%w_'] to make it handle normal stuff
	-- and extra stuff the same.
	-- This also turns hex numbers into, eg. 0Xa7d4
	str = str:gsub("(%a)([%w_']*)", tchelper)
	return str
end

local split = function(regex, str)
	local tab = {}
	local startt = 1
	local s1, e1 = string.find(str, regex, startt)
	while s1 do
		table.insert(tab, string.sub(str, startt, s1-1))
		startt = e1 + 1
		s1, e1 = string.find(str, regex, startt)
	end
	table.insert(tab, string.sub(str, startt, -1))
	return(tab)
end


local mystring = "Carolyn Carson      cares	about cars!"

print(titleCase(mystring))

local tab = split("%s+", mystring)

for k, v in ipairs(tab) do
	print(tostring(k) .. ":" .. tostring(v))
end
print()

tab = split("[Cc]ar", mystring)

for k, v in ipairs(tab) do
	print(tostring(k) .. ":" .. tostring(v))
end
print()

tab = split("o", mystring)

for k, v in ipairs(tab) do
	print(tostring(k) .. ":" .. tostring(v))
end