Dan Sopher wrote:
This works in a one-liner:

$string =~ s/^\s*(.*\S)\s*$/$1/;

Cheers!

Let's compare Dan's one-liner to the solutions in the FAQ (perlfaq4):

$ perl -le'
for ( "\nX\n", "\nX", "X\n", "X", "\n\n\n", "\n", "" ) {
    $a = $b = $c = $_;

    $d  = $a =~ s/^\s*(.*\S)\s*$/$1/;

    $e  = $b =~ s/^\s+//;
    $e += $b =~ s/\s+$//;

    $f  = $c =~ s/^\s+|\s+$//g;

    print "Test: ", ++$g," Length of original: ", length( $_ ), "\n",
"Dan\047s length: ", length( $a ), " on a string that was", $d ? "" : " NOT", " modified.\n", "FAQ 1 length: ", length( $b ), " on a string that was", $e ? "" : " NOT", " modified.\n", "FAQ 2 length: ", length( $c ), " on a string that was", $f ? "" : " NOT", " modified.\n";
    }
'
Test: 1 Length of original: 3
Dan's length: 1 on a string that was modified.
FAQ 1 length: 1 on a string that was modified.
FAQ 2 length: 1 on a string that was modified.

Test: 2 Length of original: 2
Dan's length: 1 on a string that was modified.
FAQ 1 length: 1 on a string that was modified.
FAQ 2 length: 1 on a string that was modified.

Test: 3 Length of original: 2
Dan's length: 1 on a string that was modified.
FAQ 1 length: 1 on a string that was modified.
FAQ 2 length: 1 on a string that was modified.

Test: 4 Length of original: 1
Dan's length: 1 on a string that was modified.
FAQ 1 length: 1 on a string that was NOT modified.
FAQ 2 length: 1 on a string that was NOT modified.

Test: 5 Length of original: 3
Dan's length: 3 on a string that was NOT modified.
FAQ 1 length: 0 on a string that was modified.
FAQ 2 length: 0 on a string that was modified.

Test: 6 Length of original: 1
Dan's length: 1 on a string that was NOT modified.
FAQ 1 length: 0 on a string that was modified.
FAQ 2 length: 0 on a string that was modified.

Test: 7 Length of original: 0
Dan's length: 0 on a string that was NOT modified.
FAQ 1 length: 0 on a string that was NOT modified.
FAQ 2 length: 0 on a string that was NOT modified.



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to