Thread Tools Display Modes
03-24-10, 09:00 AM   #1
suicidalkatt
A Rage Talon Dragon Guard
 
suicidalkatt's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 331
String to Roman Numberal

I'm trying to find a good way to change a string "Rank #" to a roman numberal.

The number should not be more than 10 so I'm fine with converting a number a roman numberal.

However I'm not incredibly sure of what I'm doing with pattern matching.

Code:
local function stringToRoman(string)
	local number = string.find(string, "%d")
	if number == 1 then
		return ""
	elseif number == 2 then
		return " II"
	elseif number == 3 then
		return " III"
	elseif number == 4 then
		return " IV" 
	elseif number == 5 then
		return " V"
	elseif number == 6 then
		return " VI"
	elseif number == 7 then
		return " VII" 
	elseif number == 8 then
		return " VIII"
	elseif number == 9 then
		return " IX"
	elseif number == 10 then
		return " X"
	else
		return ""
	end
end

function TotemLocales(number)
	local name, rank = GetSpellInfo(number)
	rank = stringToRoman(rank)
	print(name..rank)
end
Edit: From what I can tell it's basically returning 6 every time and is probably a result of what character is the first number of the string?

The number is always the 6th character of the string.
Rank_4

Last edited by suicidalkatt : 03-24-10 at 09:07 AM.
  Reply With Quote
03-24-10, 09:13 AM   #2
xConStruct
A Chromatic Dragonspawn
 
xConStruct's Avatar
AddOn Author - Click to view addons
Join Date: May 2008
Posts: 199
string.find returns the position where the match occured, like you suspected.

You're looking for:
local number = string:match("(%d+)")
__________________
« Website | GitHub »

Oh hai!
  Reply With Quote
03-24-10, 09:36 AM   #3
suicidalkatt
A Rage Talon Dragon Guard
 
suicidalkatt's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 331
Originally Posted by Cargor View Post
string.find returns the position where the match occured, like you suspected.

You're looking for:
local number = string:match("(%d+)")
Thanks :P

Code:
local function stringToRoman(string)
	local number = string.match(string, ("%d+"))
	if number == "1" then
		return ""
	elseif number == "2" then
		return " II"
	elseif number == "3" then
		return " III"
	elseif number == "4" then
		return " IV" 
	elseif number == "5" then
		return " V"
	elseif number == "6" then
		return " VI"
	elseif number == "7" then
		return " VII" 
	elseif number == "8" then
		return " VIII"
	elseif number == "9" then
		return " IX"
	elseif number == "10" then
		return " X"
	else
		return " invalid return"
	end
end

function TotemLocales(number)
	local name, rank = GetSpellInfo(number)
	rank = stringToRoman(rank)
	print(name..rank)
end
Now returns a spell name and rank for totems wooT!
  Reply With Quote
03-24-10, 10:07 AM   #4
Waverian
A Chromatic Dragonspawn
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 188
Code:
local roman = {
	-- using an array with implicit keys
	"",
	"II",
	"III",
	"IV",
	"V",
	"VI",
	"VII",
	"VIII",
	"IX",
	"X",
}

function TotemLocales(spellID)
	local name, rank = GetSpellInfo(spellID)
	print(("%s %s"):format(name, roman[rank]))
end
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » String to Roman Numberal


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off