in reply to Efficient trimming of trailing whitespace from array elements?

map {s/\s+$//; $_} @data
should do nicely. The result is a list that contains the original elements minus trailing whitespace. Note the $_: this gives you the string rather than the output of the regexp which would be just the number of substitutions.

Hope this helps, -gjb-

Replies are listed 'Best First'.
Re^2: RegEx needed
by diotalevi (Canon) on Dec 31, 2002 at 16:07 UTC

    Did you mean to make a copy? Just trimming it is better done by a for loop: s/\s+$// for @data;. That fixes the existing array in place and would prevent such abortions as @data = map { s/\s+$//; $_ } @data which are all too likely given your original suggestion.


    Fun Fun Fun in the Fluffy Chair

      If I'd suggested a for someone would have pointed out it's not Perlish ;-) Yes, I'm aware I'm making a copy. If one is concerned about performance though, it would be better to initialize the array with data not containing the trailing whitespace in the first place rather than having to fix it afterwards.

      Just my 2 cents, -gjb-