Forum

> > CS2D > Scripts > Some string.sub questions
Forums overviewCS2D overview Scripts overviewLog in to reply

English Some string.sub questions

6 replies
To the start Previous 1 Next To the start

old Some string.sub questions

KenVo
User Off Offline

Quote
I do this:
1
2
3
usgn = 14706
boo = usgn.."hahaha"
print(string.sub(boo,1,5))

How can i make it to prints 14706 but also could make it prints usgns that has smaller than 5 digits like 1234, 123,12,1

old Re: Some string.sub questions

Lee
Moderator Off Offline

Quote
meh, since usgn is a number, you'll have to do

1
string.sub(boo, 1, math.log(usgn,10)) -- the second parameter is automatically math.floor()ed
or
1
string.sub(boo, 1, #tostring(usgn))

A little bit of runtime analysis

math.log runs in log time (the c version for natural log does is O(log n) so log base 10 is O(2*log n) = O(log n))

tostring(usgn) also runs in log time (in c, itoa is log n, which is intuitionistic)
and #list runs in O(n) time, so the over complexity of the second one is O(log^2 n) (where the n in #list is log n in this sense), which is larger asymptotically. Since lua numbers are bounded by 64 bytes, this doesn't really matter (C1*64 vs C2*64*64 is trivial)
edited 1×, last 04.04.11 08:45:52 pm

old Re: Some string.sub questions

KimKat
GAME BANNED Off Offline

Quote
You want to display your USGN ID as a say command? here's how you do it.
1
2
3
4
5
6
7
8
9
addhook("say","on_say")
function on_say(p,txt)
	local usgn = player(p,"usgn")
	if txt=="!usgn" then
		msg2(p,"©255255000My USGN ID is "..usgn)
	elseif usgn==0 then -- If it's a guest player with no USGN ID it will not be displayed.
		return 1
	end
end
This should work accurately.

Simply type !usgn in chat and it'll display in yellow text.
edited 1×, last 04.04.11 10:35:06 pm

old Re: Some string.sub questions

KenVo
User Off Offline

Quote
Thank you guys for helping me, I understand now
And Kimkat I already know how to do that but thank you anyways cookie
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview