since this is such a commonly done affair... its best to define small functions, say trim (to remove leading and trailing whitespace), trimstart (to remove leading whitespace only ), and trimend (to remove trailing whitespace only), and use them again and again. I have had to do this quite often and find these functions quite useful...Here are the functions, as I use them, as part of a module where I keep my commonly used functions.
use strict; use Data::Dumper; my $line1 = " first "; my $line2 = " second "; print "\nlines to be trimmed...\n"; print Dumper $line1; print Dumper $line2; $line1 = trim ($line1); $line2 = trim ($line2); print "\nafter trimming start and end both...\n"; print Dumper $line1; print Dumper $line2; # in case u have an array.. you can use the map function # say my @array; # and stuff with spaces at the beginning or end or both $line1 = " first "; $line2 = " second "; print "\nlines to be trimmed...\n"; print Dumper $line1; print Dumper $line2; push (@array,$line1); push (@array,$line2); print "\narray to be trimmed...\n"; print Dumper @array; my @array2 = map { trim ($_) } @array; print "\ntrimmed array\n"; print Dumper @array2; # using trimstart and trimend $line1 = " first "; $line2 = " second "; print "\nlines to be trimmed...\n"; print Dumper $line1; print Dumper $line2; $line1 = trimstart ($line1); $line2 = trimend ($line2); print "\ntrimming the start using trimstart...\n"; print Dumper $line1; print "\ntrimming the end using trimend...\n"; print Dumper $line2; sub trim { # remove both leading and trailing whitespace my $line = shift; $line=~s/^\s+|\s+$//g; return $line; } sub trimstart { # remove leading whitespace my $line = shift; $line=~s/^\s+//g; return $line; } sub trimend { # remove trailing whitespace my $line = shift; $line=~s/\s+$//g; return $line; }

perliff

----------------------

-with perl on my side


In reply to Re: How to remove trailing space ? by perliff
in thread How to remove trailing space ? by bh_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.