The highest rated answer here looked great, but it, IMHO, is not as easy to read as it could be. I added a line with internal spaces to show that they are not removed.
#!/usr/bin/perl use strict; use warnings; my @array = ("shayam "," Ram "," 24.0 ", " foo bar garply "); map { s/^\s+|\s+$//g; } @array; for my $element (@array) { print ">$element<\n"; }
Exit:
>shayam< >Ram< >24.0< >foo bar garply<
Eric Van Bezooijen
source share