Perl (Scripting) - Remove empty lines from array

by
Jeremy Canfield |
Updated: March 09 2020
| Perl (Scripting) articles
Let's say you have an array of data that contains empty lines, like this.
Hello
World
How
are
you
The following markup can be used to remove the empty lines from the array. The \S regular expression means "anything except for a whitespace". It may also be noteworthy that \s means "whitespace", thus \S is the opposite of \s.
@array = grep /\S/, @array;
Which will result in the array having no empty lines, like this.
Hello
World
How
are
you
Looping through array
If you are looping through the array, something like this should get the job done.
foreach my $line (@array) {
$line =~ s|^\s*$||g;
}
Did you find this article helpful?
If so, consider buying me a coffee over at