$array1[$k] =~ s/^\s+//; $array1[$k] =~ s/\s+$//; $array2[$l] =~ s/^\s+//; $array2[$l] =~ s/\s+$//;
Maybe preferably use a little helper routine (or even existing code from a module such as Text::Trim):
# modify value in place sub trim { $_[0] =~ s/^\s+//; $_[0] =~ s/\s+$//; } trim($array1[$k]); trim($array2[$l]); # --- or --- # return the trimmed value sub trim { my $s = shift; $s =~ s/^\s+//; $s =~ s/\s+$//; return $s; } $array1[$k] = trim($array1[$k]); $array2[$l] = trim($array2[$l]);
This has the advantage of documenting what's going on (with no extra effort), and if you need to trim a third or fourth time, the code would eventually get more compact. Also, should you ever want to modify the regex, you'll only have to do it one place, so you'll not risk suffering from the cut-n-paste syndrome (one characteristic of which is that it's not only tedious, but also easy to overlook one of the several supposed-to-be-identical code fragments scattered about, when making modifications).
Also, generally try to avoid the variable name $l.
With many fonts, it's difficult to discern $l (dollar-ell)
from $1 (dollar-one)...
</nitpick>
In reply to Re^2: compare 2 arrays of strings
by almut
in thread compare 2 arrays of strings
by sharan
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |