Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have an array full of elements that might possibly have trailing whitesapce after the names.

Is there an efficient reg ex I could use to remove all the trailing white space if any

Thanks

Title edit by tye

  • Comment on Efficient trimming of trailing whitespace from array elements?

Replies are listed 'Best First'.
Re: RegEx needed
by krujos (Curate) on Dec 31, 2002 at 15:50 UTC
    perldoc has a few answers to this too, you should check them out. This should work for you though.
    s/(\s+)$//;

      s/(\s+)$//;

      You can leave out the parentheses. (Unless you need $1, but you don't, in this case)

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

Re: RegEx needed
by Wonko the sane (Curate) on Dec 31, 2002 at 16:17 UTC
    Here is a way that you can get a list of all elements, without any leading OR trailing whitespace.
    This is works even if the element itself has whitespace in the middle.
    my @trimmed_array = map { /^\s*(.+?)\s*$/ } @array;

    In place editing of the array would prob be prefered though
    s/\s*$// for(@array);

    Wonko
Re: Efficient trimming of trailing whitespace from array elements?
by MarkM (Curate) on Dec 31, 2002 at 21:02 UTC

    With a subtle difference from suggestions made by others, I suggest that the removal of whitespace from the end of all array elements should be accomplished using the code:

    s/\s+\z// for @ARRAY;

    I choose to use '\s+\z' instead of '\s+$' as '\s+$' means "all whitespace up to the end-of-string but not including a possible newline at the end-of-string." '\s+$' works in that \s+ is greedy and grabs the newline, however, the statement is not as clear. '\s+\z' means "all whitespace up to the end-of-string".

Re: RegEx needed
by gjb (Vicar) on Dec 31, 2002 at 15:55 UTC

    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-

      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-

Re: RegEx needed
by waswas-fng (Curate) on Dec 31, 2002 at 16:21 UTC
    Edited: Ignore this I had just woke up and had no coffe yet -- I misread the posts. BAD -> Or better yet, chomp as you create the array.

    -Waswas