techie
techie

Reputation: 59

How to remove array elements like white spaces and other characters using perl

How to remove array elements like white spaces and some character other the numbers and alphabets? I tried the below code. It still prints all the white space and some characters other than numbers and alphabets.

my $j=0;
while (my $line = <$file>) {
my $string = substr($line, 0, 10);
@array[$j] = $string;
$j++;
}
s/^\s+|\s+\z//g for @array;
foreach my $element (@array) {
print "$element\n";
}

Upvotes: 0

Views: 270

Answers (1)

Timur Shtatland
Timur Shtatland

Reputation: 12377

EDIT 2 (after more OP's comments below):

(Quoting slightly rephrased OP's post + comments):

Keep only the array elements which have one or more letter or digit. Remove all other leading and trailing characters.

The complete solution, which is both shorter, more robust and more perl-ish, is grep and map with multiple operations all packed inside.

@array = grep { $_ ne q{} } 
         map {
                $_ = substr $_, 0, 10;
                s{\A[^A-Za-z0-9]+|[^A-Za-z0-9]+\z}{}g;
                $_
         } <$file>;

Details:

<$file> returns the entire file contents as an array.
map operates on each element, aliased to $_ by default. It returns $_ after changes - that's why $_ is explicitly added at the end, as the last thing map evaluates.
\A is the beginning of the line.
\z is the end of the line.
[^A-Za-z0-9]+ is any non-{letter or digit} character repeated 1 or more times.
grep { $_ ne q{} } removes empty strings.

Why s/^\s+|\s+\z//g for @array; isn't working?

This removes leading and trailing whitespace only. It does not remove other non-{digit or letter} characters, such as '.' or '_'. It also does not remove empty strings from the array, such as those resulting from all-whitespace array elements (e.g., "\t \t") that have been replaced with an empty string: "". For example, this regex substitution will change this 2 element array: (" . a. ", "\t \t") into this 2 element array: (". a.", "") - not what you want.

SEE ALSO:

The docs for Perl regular expressions, map and grep have more details.

Upvotes: 3

Related Questions