Page 1 of 1

Shortest way to remove empty lines?

Posted: 08 Sep 2011 17:59
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.

Re: Shortest way to remove empty lines?

Posted: 08 Sep 2011 18:41
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";

Re: Shortest way to remove empty lines?

Posted: 08 Sep 2011 19:03
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.