PtokaX forum

Development Section => Your Developing Problems => Topic started by: NightLitch on 16 February, 2004, 17:10:28

Title: Help witch gsub plz guys....
Post by: NightLitch on 16 February, 2004, 17:10:28
This is my single catching [VAL] code:
function Lang(string,value1)
lang=gsub(string,"%b[]", value1)
return lang
end

how do I make it catch all the [] in a sentence ???

ex:

Hello [USER] your IP is [IP] and you are sharing [SHARE]

how will this be done in above code.

help me out plz.

Noob on gsub.

/NL
Title:
Post by: kepp on 16 February, 2004, 18:43:31
i have no idea what you mean but ?

data = gsub(data,"%b[USER]",value1)
data = gsub(data,"%b[IP]",value2)
data = gsub(data,"%b[SHARE]",value3)
Title:
Post by: NightLitch on 17 February, 2004, 11:33:04
Plop, Wombat, Tezlo, Chilla could you guys help me...

Kepp's sol. didn't work so good.

wanna get this working but don't know a thing about gsub.

plz / NL
Title:
Post by: c h i l l a on 17 February, 2004, 12:01:29
one way , stripped from LIS

no there are many ways ;).

kepp's only need s alittle modifing to works, well it works depending on what purpose
Title:
Post by: kepp on 17 February, 2004, 12:26:33
well i think the problem is that
you have let's say a line like this

LINE = "Hello [USER], Your IP is [IP], and you are sharing [SHARE]"

value = user.sName
value1 = user.sIP

You grab the user's Share and set that to value2..

LINE = gsub(LINE,"[USER]",value)
print(LINE)

Output : Hello user.sName, Your IP is [IP], and you are sharing [SHARE]

and you do like that till you catch all of them, might be some easier way..
I've never actually dealt with gsub so much :)
Title:
Post by: c h i l l a on 17 February, 2004, 12:53:57
or

LINE = gsub(LINE,"%[USER%]",value)

depending on the purpose ;)
Title:
Post by: tezlo on 17 February, 2004, 15:23:04
or..
welcomemsg = " hey [username] ([userip]) you are sharing [usershare]b"

uservars = {
username = function(user)
return user.sName
end,
userip = function(user)
return user.sIP
end,
usershare = function(user)
local s, e, share = strfind(user.sMyInfoString, "%$(%d+)%$%|$")
return s and tonumber(share)
end,
-- and so on
}

function dolang(user, str)
str = gsub(str, "%[(%a+)%]", function(id)
local func = uservars[id]
if func then
return func(%user)
else
return id
end
end); return str
end

function NewUserConnected(user)
user:SendData(dolang(user, welcomemsg))
end
Title:
Post by: NightLitch on 17 February, 2004, 15:42:58
Thx tezlo, but don't get all.

The function should be for all my text its not only for "user" could be for ex. my userinfo to. how do I make this right. with my above code.

Your code looked like I could use.

Will try it when I come home from my parents.

But more about gsub guys plz. Everything is good.

more more more.... :-) plz...

/NL
Title:
Post by: VidFamne on 17 February, 2004, 22:39:55
Isnt it so, that gsub only can replace one value at a time?
My guess is that you had to use strfind, and then substitute each value with gsub.
Only my two cent :))
Title:
Post by: tezlo on 17 February, 2004, 22:44:13
manual reads..
gsub (s, pat, repl [, n])
Returns a copy of s in which all occurrences of the pattern pat have been replaced by a replacement string specified by repl.

nl.. just make a function for it in uservars
Title:
Post by: VidFamne on 17 February, 2004, 23:44:55
Yepp, you are right, as usual. :)
Forgotten how powerful gsub is.
Sorry for my interference.
Title:
Post by: plop on 18 February, 2004, 01:32:36
check crazy bot and the next example (which is a mod from a piece of crazy).
OutputString = gsub(InputString, "(%S+)", function(word) return replace(word) end)

function Replace(word)
   if TableWichHoldsWhatYouWannaReplace[word] then
      word = TableWichHoldsWhatYouWannaReplace[word]
   end
   return word
end
with this you can replace every word in a string 1 by 1.

plop
Title:
Post by: NightLitch on 18 February, 2004, 01:38:00
thx alot guys. understand more now..
Title:
Post by: pHaTTy on 18 February, 2004, 01:43:54
hmm plzzzzzzzz help lol

>>> in gekko ass is replaced with another word

but if someone says pass then it changes the ass bit


how can i fix this so it only does it if its its by itself on one word
Title:
Post by: plop on 18 February, 2004, 05:07:08
QuoteOriginally posted by (uk-kingdom)pH?tt?
hmm plzzzzzzzz help lol

>>> in gekko ass is replaced with another word

but if someone says pass then it changes the ass bit


how can i fix this so it only does it if its its by itself on one word
check the hint i gave for NL, works for U 2.
my guess it that you now use a seperate gsub for everything you want 2 replace.
by using a function u can make that into 1.

plop
Title:
Post by: pHaTTy on 18 February, 2004, 05:26:42
QuoteOriginally posted by plop
QuoteOriginally posted by (uk-kingdom)pH?tt?
hmm plzzzzzzzz help lol

>>> in gekko ass is replaced with another word

but if someone says pass then it changes the ass bit


how can i fix this so it only does it if its its by itself on one word
check the hint i gave for NL, works for U 2.
my guess it that you now use a seperate gsub for everything you want 2 replace.
by using a function u can make that into 1.

plop

no i use a table

["word"]="whatever",

but ill hvae a look yep :)
Title:
Post by: Skrollster on 18 February, 2004, 09:37:42
I have realy forgotten how powerfull this language can be...

thank you tezlo for refreshing my mind :)
Title:
Post by: NightLitch on 19 February, 2004, 00:53:43
My solusion for my problem.

function Arg(str,val1,line1,val2,line2,val3,line3,val4,line4,val5,line5)
if line1 and val1 then str = gsub(str,"%b["..line1.."]",val1) end
if line2 and val2 then str = gsub(str,"%b["..line2.."]",val2) end
if line3 and val3 then str = gsub(str,"%b["..line3.."]",val3) end
if line4 and val4 then str = gsub(str,"%b["..line4.."]",val4) end
if line5 and val5 then str = gsub(str,"%b["..line5.."]",val5) end
return str
end

Is find this easy. Ok there is some "writings" but that it always will be.

Thx for all the help / NL