How do I find the first letter of a string?

Perksplus
Posts: 3
Joined: Fri Nov 19, 2010 5:39 am

How do I find the first letter of a string?

Post by Perksplus »

So I have the string "1883" I need to set a viarable to the first letter (which happens to be a number). How do I go about doing this?

The goal
- to break down this string into individual pieces that represent actions for my monk

what I have so far..

An alias that grabed this number, and sent it to my combo function (Do I have to convert this to a string first in mudlet?). The number/string is stored in "initial".

and, the combo function:

Code: Select all

function combo(initial)
a = ""
b = ""
c = ""
d = ""

if a == "1" then
guard("left leg")
end

if a == "3" then
guard("right leg")
end

if a == "4" then
guard("left arm")
end

if a == "5" then
guard("torso")
end

if a == "6" then
guard("right arm")
end

if a == "8" then
guard("head")
end

---Done with a -------

if b == "9" and c == "" then
c = "8"
d = "8"
end

if b == "3" then
send ("snk " .. target .. " right")
end

if b == "1" then
send ("snk " .. target .. " left")
end

if b == "6" then
send ( "mnk " .. target .. " right" )
end

if b == "4" then
send ( "mnk " .. target .. " left" )
end

if b == "8" then
send ("wwk " .. target )
end

if b == "5" then
send ( "sdk " .. target )
end

if b == "9" then
send ( "axk " .. target )
end

if c == "" then
c = b
c = b
end

------Done with b---------------


if c == "" and b == "9" then
c = "8"
d = "8"
end

if c == "6" then
send ("hfp " .. target .. " right")
end

if c == "1" then
send ("hfp " .. target .. " left")
end

if c == "6" then
send ( "spp " .. target .. " right" )
end

if c == "4" then
send ( "spp " .. target .. " left" )
end

if c == "8" then
send ("ucp " .. target )
end

if c == "5" then
send ( "hkp " .. target )
end

if d == "" then
d = c
end

--------Done with c-----------

if d == "3" then
send ("hfp " .. target .. " right")
end

if d == "1" then
send ("hfp " .. target .. " left")
end

if d == "6" then
send ( "spp " .. target .. " right" )
end

if d == "4" then
send ( "spp " .. target .. " left" )
end

if d == "8" then
send ("ucp " .. target )
end

if d == "5" then
send ( "hkp " .. target )
end

-----------Done with D---------

a = ""
b = ""
c = ""
d = ""

end
What I need is a way to get the first digit into a, second into b, and so forth.

So if I send the command 1883. my monk will:
-guard his left leg
-kick their head,
-punch their head
-punch their right leg.

Also If I only send 18 he will:
-guard left leg
-full combo to their head.

please help!

Yetzederixx
Posts: 186
Joined: Sun Nov 14, 2010 5:57 am

Re: How do I find the first letter of a string?

Post by Yetzederixx »

I made an alias to test this and it works, left in the debug stuff for you as well.

Pattern: ^test (\d+)$
Code: [show] | [select all] lua
local num = matches[2]
local bits = {}
local numbits = string.len(matches[2])

echo(numbits .. "\n")

-- I assume that if you only send 2 nums in it means the same thing every time
if (numbits == 2) then
	bits[1] = math.floor(num / 10)
	num = num % 10

	echo(bits[1] .. " " .. num)
	-- command 1 based of bits[1]
	-- command 2 based of num
else
	bits[1] = math.floor(num / 1000)
	num = num % 1000
	bits[2] = math.floor(num / 100)
	num = num % 100
	bits[3] = math.floor(num / 10)
	num = num % 10

	echo(bits[1] .. " " .. bits[2] .. " " .. bits[3] .. " " .. num)

	-- commands based off bits[1]-bits[3]
	-- command based off num	
end

Perksplus
Posts: 3
Joined: Fri Nov 19, 2010 5:39 am

Re: How do I find the first letter of a string?

Post by Perksplus »

You sure you're not making this a little complicated?

all I need is something like this:

string = "1984"

a = theNumberAt(string, position1) --(1)
b = theNumberAt(string, position2) --(9)

There seriously is no exsisting function that can do this?

Yetzederixx
Posts: 186
Joined: Sun Nov 14, 2010 5:57 am

Re: How do I find the first letter of a string?

Post by Yetzederixx »

There is no string.charAt(string, position) fn in Lua, I looked in the manualand Programming in Lua book.

Reguardless, this accomplishes the goal.

User avatar
Omit
Posts: 190
Joined: Sun Aug 01, 2010 10:54 pm
Location: Middle Earth
Contact:

Re: How do I find the first letter of a string?

Post by Omit »

Code: [show] | [select all] lua
a = string.sub(yourstring,1,1)  
b = string.sub(yourstring,2,2)  
c = string.sub(yourstring,3,3)  
d = string.sub(yourstring,4,4)  
echo("A="..a.." B="..b.." C="..c.." D="..d)
try that :D (check out Lua's string functions... very useful)

Yetzederixx
Posts: 186
Joined: Sun Nov 14, 2010 5:57 am

Re: How do I find the first letter of a string?

Post by Yetzederixx »

That would of been way to easy! Thanks!

Perksplus
Posts: 3
Joined: Fri Nov 19, 2010 5:39 am

Re: How do I find the first letter of a string?

Post by Perksplus »

hah! Thanks omit! Also, Thanks yetz... But that was way beyond me. I tried to get it to work anyways. I got some screw behaviour out of it.

If the input was 12 the output was 1 2 0 0, but if i put in 123 the output was 0 3 2 1.

Yetzederixx
Posts: 186
Joined: Sun Nov 14, 2010 5:57 am

Re: How do I find the first letter of a string?

Post by Yetzederixx »

I got my code to work perfectly before I posted it. That is what you have to do when dealing with numbers in a strongly typed language, pain in the butt, I wonder why Lua doesn't allow you to refer to a string like a table honestly, it would be so much simpler.

User avatar
Omit
Posts: 190
Joined: Sun Aug 01, 2010 10:54 pm
Location: Middle Earth
Contact:

Re: How do I find the first letter of a string?

Post by Omit »

The only problem with the way I did it would come if you wanted to use the result as a number. If you took the result and *1 the result of that is a number but.... there are times handling a number as a string can cause you to end up with a result like "1" instead of 1. (just be aware... I think the function to force a string to a number is toNumber()... or something like that)

Yetzederixx
Posts: 186
Joined: Sun Nov 14, 2010 5:57 am

Re: How do I find the first letter of a string?

Post by Yetzederixx »

I just checked, it is tonumber(num, base) where base is optional if you want it in base 2 (binary) or 16 (hexadecimal), default is 10 which is what you normally use.

Post Reply