in reply to How to delete trailing spaces from array elements?
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 ' ' }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to delete trailing spaces from array elements?
by ysth (Canon) on Jul 31, 2007 at 19:25 UTC | |
by radiantmatrix (Parson) on Aug 02, 2007 at 17:16 UTC |