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

Hi Brethren
I'm capturing the space after a full stop.
How do I get rid of it?
I've tried any combination of below but can't shift it!
while(<IN>) { $yline=$_; chomp($yline); $yline =~ s/\cM\cJ|\cM|\cJ/ /g; #returns et al $yline =~ s/^\s+//g; #Leading spaces $yline =~ s/\s\s+/ /g; #Trailing spaces @yline = split (/(?<=\.)/, $yline); #splits line but retains full + stop #@yline = split ($yline =~ /(.*?(?:\.|$))\s*/g);#grabs anything af +ter #@yline = split (/\./, $yline); foreach(@yline){ print "$_\n"; }

Replies are listed 'Best First'.
Re: Capturing Space with Split
by Corion (Patriarch) on Mar 13, 2006 at 20:02 UTC

    Hello Gavin and welcome to the monastery!

    In your previous questions, you were already given helpfull hints, so maybe you can tell us more about how those responses do not work for you. Please read How (not) to ask a question and supply an explanation what you expect the code to do, your input data, and the output you get from it.

    Please also refer to your earlier questions instead of posting a new question which is very similar to the questions you already posted.

Re: Capturing Space with Split
by GrandFather (Saint) on Mar 13, 2006 at 20:14 UTC

    Listen to the Brethren - show us your problem! The code below is taken from I know what I mean. Why don't you? and demonstrates a very simple way to include test data in your post to demonstrate a problem.

    while (<DATA>) { print $_; } __DATA__ Hello world

    Let's edit that a little to perhaps get closer to the question you want to ask:

    use warnings; use strict; while (<DATA>) { chomp; my @sentences = split /(?<=\.)\s*/; print '>', join ("<\n>", @sentences), '<'; } __DATA__ Hello world. Hello Bretheren. Goodbye spaces. All I really want are se +ntences.

    Which prints:

    >Hello world.< >Hello Bretheren.< >Goodbye spaces.< >All I really want are sentences.<

    Now, your job is to modify that to demonstrate the problem you are having and to show us the output you expect to get.


    DWIM is Perl's answer to Gödel
      Perhaps I could have been more specific at the outset, but explaining what you would like the code to do when you are not very sure yourself how to go about the task is rather difficult!
      use warnings; use strict; while (<DATA>) { chomp; my @sentences = split /(?<=\.)\s*/; #print '>', join ("<\n>", @sentences), '<'; foreach(@sentences){ print "$_\n"; } } __DATA__ Hello world. Hello Bretheren. Goodbye spaces. All I really want are se +ntences. [download] Which prints: Hello world. Hello Bretheren. Goodbye spaces. All I really want are sentences. Your code >Hello world.< >Hello Bretheren.< >Goodbye spaces.< >All I really want are sentences.< I would like: Hello world. Hello Bretheren. Goodbye spaces. All I really want are sentences.
      Your code print '>', join ("<\n>", @sentences), '<'; does exactly what I want except for the > < I would like the data without the space to left after the first line.

        If I understand your reply, you now have a solution to your problem. The "trick" was to gobble up the spaces following the full stop with the \s* in split's regex: split /(?<=\.)\s*/.

        The reason for the angle brackets around the text was to demonstrate that there were no hidden spaces - white space can be hard to see. Printing without them is as simple as: print join "\n", @sentences;.

        While you are exploring the Monastery I strongly recommend that you wander into the tutorials section and have a good browse there. There is a lot of material that should help someone starting out on their Perl journey.

        Oh, and welcome to The Monastery.


        DWIM is Perl's answer to Gödel
Re: Capturing Space with Split
by thundergnat (Deacon) on Mar 13, 2006 at 21:00 UTC
    $yline =~ s/\cM\cJ|\cM|\cJ/ /g;

    That line is going to do absolutely nothing for you unless you change your input record separator as I pointed out before.