use strict; use warnings; my @phrases = ("hi there", " this and that ", " and the other thing", "blah blah blah ", "one two there", " four five six "); @phrases = map { trim($_) } @phrases; print join("\n", @phrases); sub trim { my $string = shift; $string =~ s/^\s+//; #trim leading space $string =~ s/\s+$//; #trim trailing space return $string; }