in reply to How to clean an array of strings after a split

G'day MrSnrub,

There's a couple of problems here: substitution is s/PATTERN/REPLACEMENT/ (note the s///); the 'g' modifier is for handling multiple matches (you're only dealing with a single match here). See perlretut for the basics and perlre for the details.

You don't need to do two separate substitutions. This regex will suffice:

s/^\s*(.*?)\s*$/$1/

Note that $_ ||= "" will convert zeros (being FALSE values) into empty strings! Did you want to do that?.

Also, unless you're making multiple calls to Trim(), you can just do the substitution within the map. Here's my test (where I've also attempted to make the split a little clearer):

$ perl -Mwarnings -Mstrict -E ' my $str = " a | b|c |d|0| "; my @fields = map { $_ ||= ""; s/^\s*(.*?)\s*$/$1/; $_; } split /[| +]/ => $str; say ">>>$_<<<" for @fields; ' >>>a<<< >>>b<<< >>>c<<< >>>d<<< >>><<< >>><<<

-- Ken