stringConvert - Setting a new settings standard!
 

stringConvert - Setting a new settings standard!

Started by Corayzon, 05 May, 2004, 06:12:32

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Corayzon

hey guys...i just finished the complete version of my translatestring function...
and i think u guys should start to use something similar to make creating editable settings for clients easier!

Example:
sSetting = "welcome #user at #ip to #hubname"
user:SendData("someshit", stringConvert(sSetting, "#user", user.sName, "#ip", user.sIP, "#hubname", frmHub:GetHubName())
will return ' welcome Corayzon at 192.168.0.1 to cSlave Test Hub'

hope u guys find it usefull!

function stringConvert(sString, s1, sr1, s2, sr2, s3, sr3, s4, sr4, s5, sr5, s6, sr6, s7, sr7, s8, sr8, s9, sr9, s10, sr10, s11, sr11, s12, sr12, s13, sr13, s14, sr14)
	
	-- cSlave Extra's
	sString, _ = gsub(sString, "#hd",frmHub:GetHubDescr())
	sString, _ = gsub(sString, "#uc", frmHub:GetUsersCount())
	sString, _ = gsub(sString, "#hb", frmHub:GetHubBotName())
	sString, _ = gsub(sString, "#mu", frmHub:GetMaxUsers())
	sString, _ = gsub(sString, "#ms", convertShareSize(frmHub:GetMinShare(), 2))
	sString, _ = gsub(sString, "#op", frmHub:GetOpChatName())
	sString, _ = gsub(sString, "#ra", frmHub:GetRedirectAddress() or "none")
	sString, _ = gsub(sString, "#hs", convertShareSize(frmHub:GetCurrentShareAmount()))
	sString, _ = gsub(sString, "#h", frmHub:GetHubName())
	sString, _ = gsub(sString, "#new", "\r\n")
	sString, _ = gsub(sString, "#tab", "\t")

	-- Input Strings
	if s1 ~= nil and sr1 ~= nil then sString, _ = gsub(sString, s1, sr1 or "") end
	if s2 ~= nil and sr2 ~= nil then sString, _ = gsub(sString, s2, sr2 or "") end
	if s3 ~= nil and sr3 ~= nil then sString, _ = gsub(sString, s3, sr3 or "") end
	if s4 ~= nil and sr4 ~= nil then sString, _ = gsub(sString, s4, sr4 or "") end
	if s5 ~= nil and sr5 ~= nil then sString, _ = gsub(sString, s5, sr5 or "") end
	if s6 ~= nil and sr6 ~= nil then sString, _ = gsub(sString, s6, sr6 or "") end
	if s7 ~= nil and sr7 ~= nil then sString, _ = gsub(sString, s7, sr7 or "") end
	if s8 ~= nil and sr8 ~= nil then sString, _ = gsub(sString, s8, sr8 or "") end
	if s9 ~= nil and sr9 ~= nil then sString, _ = gsub(sString, s9, sr9 or "") end
	if s10 ~= nil and sr10 ~= nil then sString, _ = gsub(sString, s10, sr10 or "") end
	if s11 ~= nil and sr11 ~= nil then sString, _ = gsub(sString, s11, sr11 or "") end
	if s12 ~= nil and sr12 ~= nil then sString, _ = gsub(sString, s12, sr12 or "") end
	if s13 ~= nil and sr13 ~= nil then sString, _ = gsub(sString, s13, sr13 or "") end
	if s14 ~= nil and sr14 ~= nil then sString, _ = gsub(sString, s14, sr14 or "") end
	return sString
end

NotRabidWombat

Wow. Talk about overkill.

Ok, first thing. Lose all the strfind's condition blocks. They aren't saving you anytime. And gsub will return an unmodified string if the substring does not exist.

Secondly, use the ... for the argument list. It'll make your function a lot cleaner and lot more efficient, ie: only do gsubs based on the length of the argument array.

Thirdly, the frmHub functions will be called for each replacement. You can make these more efficient by assigning them to global values (since most of them will not change unless there is a script restart too).

Finally, what is the use case for this function? How many times would you use it rather than something like format?

9/10 people agree I'm a big jerk after reading this post.

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

Corayzon

your no jerk...makes sense to me!

i know i could write a loop to handle this with the gsub in the middle...but just cant be f'd at the moment...

1 - done!
2 - will do!
3 - um yea but no
4 - i dont understand

anyways...

i put this up so u smart guys could make it into something that is standard and used alot with ptokax side scripting....

and thankz for the time to tell me what kicking NotRabidWombat! =]

NotRabidWombat

#3
4 - I dont understand

http://www.lua.org/manual/4.0/manual.html#6.2

Read down a little to the format function.

Next question, how do I represent the actual string #hd rather than having it be replaced?

Based on recent statistical findings
8/10 people agree I'm a big jerk after reading this post.

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

Corayzon

cheerz dude!

thankz for the help alot...

i think i leant most of my shit from ya posts around here n there ;)

and...if u wanna make better just do,,,thats y i post as setting the new settings standard =]

Corayzon

QuoteOriginally posted by NotRabidWombat
Next question, how do I represent the actual string #hd rather than having it be replaced?

you talkin bout referencing the string?

NotRabidWombat

I'm talking about the obscure case where I want to actually print a special string rather than having it be replaced. Something like a magic character should take care of the issue.

-NotRabidWombat


I like childish behavior. Maybe this post will be deleted next.

Corayzon

oh yes... i see what u mean

well..ill write another version based on what u have said so far...and from there ill see what u have to say again ;)

thankz for the help dude! :D

SMF spam blocked by CleanTalk