Shortest way to remove empty lines?

Please check the FAQ (https://www.xyplorer.com/faq.php) before posting a question...
Post Reply
highend
Posts: 14955
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Shortest way to remove empty lines?

Post by highend »

After reading a file into a variable and removing the content of a few lines via a regex (for e.g. an .m3u playlist file) is there a single command which can be used to remove all these empty lines?

This is my current (ugly) version:

Code: Select all

	$PlaylistFile = readfile("<curitem>");
	
		// Remove all unnecessary data
		$CleanupPlaylist = regexreplace("$PlaylistFile", "^#.*$", "");

		// Replace the separator
		replace $CleanupPlaylist, $CleanupPlaylist, "<crlf>", "|";

		// Target linebreaks
		$Linebreaks = substr("$CleanupPlaylist", 0, 1);

		// Remove all linebreaks
		replace $UnsortedPlaylist, $CleanupPlaylist, "$Linebreaks", "";
I tried to do it with different regexpressions (\r\n$, etc.) and hoped that formatlist could do this as well (with format "e") but I haven't found an easier way yet.
One of my scripts helped you out? Please donate via Paypal

TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Shortest way to remove empty lines?

Post by TheQwerty »

I'm coding blind but give this a shot:

Code: Select all

"Filter Playlist"
    $PlaylistFile = readfile("<curitem>");

    // Remove all unnecessary data (attempt to take its line break as well)
    $CleanupPlaylist = RegexReplace("$PlaylistFile", '^#.*\r?\n?');

    // Replace one or more line breaks (including any preceding white space) with '|'
    $CleanupPlaylist = RegexReplace("$CleanupPlaylist", '\s*[\r\n]+', '|');

    // This shouldn't be needed now but just in case remove empty tokens....
    $CleanupPlaylist = FormatList("$CleanupPlaylist", 'e');
    
    Text "$CleanupPlaylist";

highend
Posts: 14955
Joined: 06 Feb 2011 00:33
Location: Win Server 2022 @100%

Re: Shortest way to remove empty lines?

Post by highend »

Thanks TheQuerty :)

Code: Select all

$CleanupPlaylist = RegexReplace("$PlaylistFile", "^\r?\n?", "");
And this one can be used to delete all (normal) empty lines in a variable.
One of my scripts helped you out? Please donate via Paypal

Post Reply