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

HI my question is simple I am trying this @arr1= (“string1.. multiplespaceshere “,”string2nospace”,”string one or moretab “); Now I want to delete trailing spaces from each element of string and store them back Note: after actual string there may be any number of blanks or tabs or notabsnospaces... As I am beginer to perl this may seem silly to experts...
  • Comment on How to delete trailing spaces from array elements?

Replies are listed 'Best First'.
Re: How to delete trailing spaces from array elements?
by GrandFather (Saint) on Jul 31, 2007 at 10:13 UTC

    A basic question maybe, but not silly. There are a couple of things that you need to learn about to solve your problem. You need to be able to iterate over the elements of the array so you should look at map and for loops.

    You need to be able to trim the trailing white space off the end of each element and the Perl tool for that is a regular expression substitution. See perlretut and perlre. Regular expressions are a very important part of Perl and repay through study.


    DWIM is Perl's answer to Gödel
Re: How to delete trailing spaces from array elements?
by wind (Priest) on Jul 31, 2007 at 10:00 UTC
      s/\s+$// for (@arr1);
      With a for loop in modifier mode, you don't need the parens. The code gets just a little bit tidier that way (IMO of course).
      s/\s+$// for @arr1;
Re: How to delete trailing spaces from array elements?
by radiantmatrix (Parson) on Jul 31, 2007 at 14:14 UTC

    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 ' ' }
    <radiant.matrix>
    Ramblings and references
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet
      for(@arr1){1while' 'eq($c=chop);$_.=$c}

        Ok, if you insist:

        $/=' ';for(@arr1){1while chomp}

        Is 8 chars shorter. ;-)

        <radiant.matrix>
        Ramblings and references
        The Code that can be seen is not the true Code
        I haven't found a problem yet that can't be solved by a well-placed trebuchet
Re: How to delete trailing spaces from array elements?
by atemon (Chaplain) on Jul 31, 2007 at 14:58 UTC

    Hi,

    Just one more way to do the same :)

    @arr1 = map{ (s/\s*$//)&&$_}@arr1;

    Cheers !

    --VC



    There are three sides to any argument.....
    your side, my side and the right side.

      The asignment is unnecssary since you've already modified @arr1 by modifying $_.
      use List::MoreUtils qw( apply ); @array = apply { s/\s+\z// } @array;

      or

      map { s/\s+\z// } @array;

      or

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

        if you don't want to dig into regular expressions (that funny /\s+$//gms stuff) yourself, you might try this approach:

        use Text::Trim; my @strings2trim = ("yo ", " boo"); trim(@strings2trim);