[SOLVED] Split string based on carriage return?

Ask about ACS, DECORATE, ZScript, or any other scripting questions here!

Moderator: GZDoom Developers

Forum rules
Before asking on how to use a ZDoom feature, read the ZDoom wiki first. If you still don't understand how to use a feature, then ask here.

Please bear in mind that the people helping you do not automatically know how much you know. You may be asked to upload your project file to look at. Don't be afraid to ask questions about what things mean, but also please be patient with the people trying to help you. (And helpers, please be patient with the person you're trying to help!)
Post Reply
User avatar
Matt
Posts: 9696
Joined: Sun Jan 04, 2004 5:37 pm
Preferred Pronouns: They/Them
Operating System Version (Optional): Debian Bullseye
Location: Gotham City SAR, Wyld-Lands of the Lotus People, Dominionist PetroConfederacy of Saudi Canadia
Contact:

[SOLVED] Split string based on carriage return?

Post by Matt »

I want to have a script that reads a plaintext file that looks like

Code: Select all

Item 1
Item 2
Item 3
etc.
and use Split() to make an array of the items, one item per line.

Is there a way to do this without actually putting a carriage return in my code? (And if I do, would it break switching between Windows and Unix?)


Kinda relatedly: Is there any way now, without cvar abuse, to reference one player's local file and then broadcast that set of data to the entire game so it doesn't desync even though each player's file is different?


EDIT: I just noticed this works fine (logs a single "a" with no whitespace):

Code: Select all

string hahahaha="a
b
c
";
array<string> aha;aha.clear();
hahahaha.split(aha,"\n");
A_Log(aha[0]);
Last edited by Matt on Sat May 25, 2019 2:07 pm, edited 2 times in total.
User avatar
phantombeta
Posts: 2088
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

Re: Split string based on carriage return?

Post by phantombeta »

You should split on line feed (\n) instead. Both Windows and Unix use it for line breaks, with Windows using "CRLF" (\r\n) and Unix using "LF" (\n). The only case where this would break would be if someone were using only carriage returns for line breaks, but that was only used by some ancient versions of Mac OS, so it's not like it really matters.

And no, I don't think there's any way to do that without using CVars or netevents.
Post Reply

Return to “Scripting”