There's a pretty common pattern for solving this, and it looks like this:
for (@arr1) { #for each element in @arr1 s/\s+$//; #replace one or more spaces at the end of it #with nothing (deleting them) }
You'll commonly see the regular expression s/^\s+|\s+$//g as well -- which basically says "match any whitespace at the beginning or end of the string, and remove all of it".
The shorter versions you see near the top of this discussion are simply shorter ways to write this pattern. In Perl, there's always more than one way to do things. To demonstrate that, here's another solution that would work (but I don't recommend):
for (@arr1) { while ( substr($_,-1,1) eq ' ' ) { # while the last char is space chop(); # remove the last char } }
Or, the shorter version:
for (@arr1) { chop() while substr($_,-1,1) eq ' ' }
In reply to Re: How to delete trailing spaces from array elements?
by radiantmatrix
in thread How to delete trailing spaces from array elements?
by rcu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |