in reply to chomp not working in array
Chomp is not the tool of choice here. You could however use a couple of regex to do the job
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; }
You will still want to use chomp to get rid of line endings, and perhaps modify trim to take an array and trim it, instead of individual strings. I'm not sure why perl doesn't include a builtin trim, perhaps they just figure its one of those "build to your own need" type tools.
|
|---|