Please help! This is probably a bug, but I cannot make sense of it.

Bale

Minion
I want to split a string into items. Pretty straightforward stuff. Then I run into the Ouija Board, Ouija Board. Still not hard, I easily split that string out with a little bit of negative lookbehind.

Then the string won't be turned into an item. It just won't. I can turn that string into an item normally, just not if I get it from split_string(). Witness the following test program!

Code:
string favgear = to_string($item[Meat Tenderizer is Murder]);
foreach it in $items[Ouija Board\, Ouija Board,Hand that Rocks the Ladle]
	favgear += ","+replace_string(to_string(it), ",", "\\,");

print("String to split: "+ favgear);
print(" ");

foreach i,fav in split_string(favgear, "\\s*(?<!\\\\),\\s*") {
	print(fav+" = "+ to_item(fav));
}
print(" ");

print("New test: Ouija Board\\, Ouija Board = " + to_item("Ouija Board\, Ouija Board"));

It produces the following output:

Code:
[COLOR="#808000"]> test[/COLOR]

String to split: Meat Tenderizer is Murder,Ouija Board\, Ouija Board,Hand that Rocks the Ladle

Meat Tenderizer is Murder = Meat Tenderizer is Murder
Ouija Board\, Ouija Board = none
Hand that Rocks the Ladle = Hand that Rocks the Ladle

New test: Ouija Board\, Ouija Board = Ouija Board, Ouija Board

WTF? Why can I transform the string into an item in the last line if it didn't work the first time?!

Here's a little sanity check in the CLI:

Code:
[COLOR="#808000"]> ashq print(to_item("Ouija Board\, Ouija Board"));[/COLOR]

Ouija Board, Ouija Board
 
Last edited:

heeheehee

Developer
Staff member
Bale, that's because the two strings you're testing aren't the same at all. One of them looks like "Ouija Board, Ouija Board"; the other looks like "Ouija Board\, Ouija Board".

In order to fix that, you'd want to do something like
Code:
print(fav+" = "+ to_item(fav.replace_string("\\,", ",")));
 

xKiv

Active member
Yup. "\," is an escaped comma, which is, depending on your language, either illegal, or just a comma.
"\\," is an escaped backslash (which is just a literal backslash), followed by a comma.
I would replace the , with something else (that also doesn't include a , in it, so you don't have to look at behinds). Perhaps "_#="? Is that likely to ever appear in item names?
 

Bale

Minion
I understand now. That makes perfect sense. I was confusing the handling of a \ inside of a variable value with its purpose in a string.

Thank you.
 
Top