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

I am getting a txt file as input from the user. Some times there are extra spaces at the end of each line. Some one pls suggest how do I remove these extra spaces at the end of each line. The entire file is stored in @lines. Thanks a lot, Sai.

Replies are listed 'Best First'.
Re: Extra spaces
by CukiMnstr (Deacon) on Jun 19, 2003 at 21:37 UTC
    this is a faq, check this.

    hope this helps,

Re: Extra spaces
by diotalevi (Canon) on Jun 19, 2003 at 21:36 UTC

    This line will remove all whitespace from the end of every element in @lines.

    s/\s+$// for @lines;
      Works great, but also deletes newlines. In case the newlines are needed:

      s/[ \t]+$// for @lines;

          -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: Extra spaces
by Enlil (Parson) on Jun 19, 2003 at 21:38 UTC
      update: update to parent node made this comment obsolete

          -- Chip Salzenberg, Free-Floating Agent of Chaos

Re: Extra spaces
by hsweet (Pilgrim) on Jun 20, 2003 at 01:19 UTC

    Also, make sure you understand in the examples here that the substitution is being done on $_. You can do something like foreach (@lines){s/whatever/whatever/} and the $_ variable will be set to your list.

    Time flies like an arrow, fruit flies like banannas