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

Hi Guys, I tried many times, but can't figure out how to remove spaces from beginning and end of sentences (if there exists spaces).
For example, if I have a sentence:
" Martina is Princess "
the result should be
"Martina is Princess"

All my sentences are in array. Some contain spaces at the beginning, some don't. I can't use the chop; function.
Thanks
  • Comment on removing space from beginning and end of sentence

Replies are listed 'Best First'.
Re: removing space from beginning and end of sentence
by tirwhan (Abbot) on Jan 28, 2006 at 09:05 UTC

    Your question, (especially the "can't use the chop function" bit) smells a lot like homework. Please make an attempt at solving your problem, then show us what you've tried and we'll point out why it's failing.

    As a further clue, take a look at perldoc perlre and the docs for the s/// operator in perldoc perlop.


    There are ten types of people: those that understand binary and those that don't.
Re: removing space from beginning and end of sentence
by PodMaster (Abbot) on Jan 28, 2006 at 09:07 UTC
    Hi. Visit Tutorials and start with How to RTFM (plenty of other tutorials to help you along your way, but this one is a good start).

    'perldoc -q space'

    Found in perlfaq4 How do I strip blank space from the beginning/end of a string? Although the simplest approach would seem to be $string =~ s/^\s*(.*?)\s*$/$1/; not only is this unnecessarily slow and destructive, it also fails + with embedded newlines. It is much faster to do this operation in two s +teps: $string =~ s/^\s+//; $string =~ s/\s+$//; Or more nicely written as: for ($string) { s/^\s+//; s/\s+$//; } This idiom takes advantage of the "foreach" loop's aliasing behavi +or to factor out common code. You can do this on several strings at once +, or arrays, or even the values of a hash if you use a slice: # trim whitespace in the scalar, the array, # and all the values in the hash foreach ($scalar, @array, @hash{keys %hash}) { s/^\s+//; s/\s+$//; }
    Simple search for remove space end yields How do I remove whitespace at the beginning or end of my string?

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: removing space from beginning and end of sentence
by davido (Cardinal) on Jan 28, 2006 at 09:15 UTC

    Do you wish to remove whitespace from the beginning and end of a sentence, line, or complete string? The implementation will be different for each of those scenarios. If from a sentence, your example text is not illustrating your problem, since " Martina is Princess " is not a complete sentence. It lacks punctuation, for one thing. Define what you mean by sentence.

    chop is not selective enough about what it chops, nor is it designed to work on the beginning of a string.

    During your "many times" of trying, what did you try?

    I recommend reading perlfaq4, particularly the section called How do I strip blank space from the beginning and end of a string?.


    Dave

Re: removing space from beginning and end of sentence
by santonegro (Scribe) on Jan 29, 2006 at 02:58 UTC
    while knowing the regexp is important, for speed and clarity in application code, consider String::Strip