WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   strsplit delimiter issue (https://www.wowinterface.com/forums/showthread.php?t=38220)

Sythalin 01-07-11 10:07 AM

strsplit delimiter issue
 
lua Code:
  1. -- CFM_LoadBox = "Blackwing Lair - Chaosinc"
  2.         b:SetScript("OnClick", function()
  3.             -- was going to prevent reloading, but it's much simpler to just do it this time
  4.             local realm, name = strsplit(" - ", UIDropDownMenu_GetText(CFM_LoadBox))
  5.             print(realm)
  6.             print(name)
  7.         end)

This is ending up printing realm, but not the name. I'm pretty sure I'm just the delimiter, but everything I've tried so far has just screwed it up more.

Seerah 01-07-11 10:37 AM

I don't see anything wrong with it...

What do you get when you do print(UIDropDownMenu_GetText(CFM_LoadBox))?

Sythalin 01-07-11 04:02 PM

Quote:

Originally Posted by Seerah (Post 225916)
I don't see anything wrong with it...

What do you get when you do print(UIDropDownMenu_GetText(CFM_LoadBox))?

Yeah, I know. It looks fine, but it doesn't like it for some reason. :(

The print() return the selected value like it should (Blackwing Lair - Chaosinc).

However, I tested this before with settings saved from my mains (Thunderlord). I just attempted the same code on my "test" realm that has two names and it prints the realm's two parts as the two returns instead.

realm = Thunderlord
returns:
Thunderlord

realm = Blackwing Lair
returns:
Blackwing
Lair

kraftman 01-07-11 08:04 PM

trying escaping the "-"

eg
" %- "

EDIT: tested it and it doesnt work :( maybe use msg:match() ?

Xinhuan 01-08-11 01:45 AM

strsplit()'s first argument must be 1 character long. If it is longer than 1 character, then ANY of the characters are treated as a delimiter. In your example

Code:

local realm, name = strsplit(" - ", UIDropDownMenu_GetText(CFM_LoadBox))
The delimeter you provided is basically " " or "-" (with " " repeated). So if you're splitting "Blackwing Lair - Chaosinc", then you end up getting the following returns:

Code:

"Blackwing", "Lair", "", "", "Chaosinc"
There are 2 empty strings there because there is nothing between " " and "-", and the second one is between "-" and " ".

If you are splitting "Barthilas - Xinhuan" then you basically get

Code:

"Barthilas", "", "", "Xinhuan"
Hence in your example code, your realm is correct, but your name is an empty string.

What you really want is this

Code:

local realm, name = string.match("Blackwing Lair - Chaosinc", "(.+) %- (.+)")

Sythalin 01-08-11 02:18 PM

Quote:

Originally Posted by Xinhuan (Post 226017)
strsplit()'s first argument must be 1 character long. If it is longer than 1 character, then ANY of the characters are treated as a delimiter. In your example

Code:

local realm, name = strsplit(" - ", UIDropDownMenu_GetText(CFM_LoadBox))
The delimeter you provided is basically " " or "-" (with " " repeated). So if you're splitting "Blackwing Lair - Chaosinc", then you end up getting the following returns:

Code:

"Blackwing", "Lair", "", "", "Chaosinc"
There are 2 empty strings there because there is nothing between " " and "-", and the second one is between "-" and " ".

If you are splitting "Barthilas - Xinhuan" then you basically get

Code:

"Barthilas", "", "", "Xinhuan"
Hence in your example code, your realm is correct, but your name is an empty string.

What you really want is this

Code:

local realm, name = string.match("Blackwing Lair - Chaosinc", "(.+) %- (.+)")

I see what you're saying there. Apparently either it wasn't noted or I missed the fact that the delimiter could only be 1 char when I was figuring out how to do this (knowing me, the latter ;)). Now I can finally get my profile section done. :D


All times are GMT -6. The time now is 05:14 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI