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

Hi! I was wondering if anyone could tell me how to remove any extra whitespaces, tabs, line breaks, etc from a string. I have a string which looks like -
12345 abcd
And I want to remove the line break and whitespaces so that it looks like -
12345 abcd
Do let me know. Thanks! :-D

Replies are listed 'Best First'.
Re: Removing whitespaces,tab,line breaks, etc?
by davorg (Chancellor) on Jan 09, 2003 at 13:43 UTC
      The s modifier is superfluous here.

        You're absolutely right of course. The clue was that /s only ever modifies the behaviour of '.' and my regex didn't include a '.'.

        --
        <http://www.dave.org.uk>

        "The first rule of Perl club is you do not talk about Perl club."
        -- Chip Salzenberg

Re: Removing whitespaces,tab,line breaks, etc?
by Zaxo (Archbishop) on Jan 09, 2003 at 13:47 UTC
    my $foo = " 12345 \t\n\t abcde"; $foo = join " ", split( " ", $foo); print $foo, $/;

    That's known as magical split.

    After Compline,
    Zaxo

Re: Removing whitespaces,tab,line breaks, etc?
by fruiture (Curate) on Jan 09, 2003 at 13:44 UTC

    See perlop for the transliteration operator:

    $_ = " 12345 \n \t ab\t\t\tcd \n "; print "Before: |$_|\n"; tr/ \t\012\015/ /s; print "After: |$_|\n";
    --
    http://fruiture.de