|
i have an array.
each element in this array is a string.
sometimes there are elements in this array which contain, say, the value "-\s\s\s\s".
in such instances i want to rid of those spacebars.
i dont want to rid of any other spacebars though.
so basically, i want to rid of all spacebars at the beginning and
at the end of each element in the list.
how do i do that?
|
|
|
you could do this.
my @stuff = ("- ", " -");
for (my $i = 0; $i <= $#stuff; $i++) {
print $stuff[$i], "1\n";
$stuff[$i] =~ s/(\A\s+|\s+\z)//g; # remover
print $stuff[$i], "2\n";
}
hope it helps
|
|
|
you may want to use this regex.
$stuff[$i] =~ s/(?:\A\s+|\s+\z)//g;
Because the (?:pattern) does not capture characters like () does and you don't want to capture characters if you don't need to.
Which will make things a little faster overall.
|
|
|
|
|
|
|
// |