in reply to Extract Paragraph From Text

G'day perlbeginneraaa,

Welcome to the Monastery.

"I expect the code returns the paragraphs, however, I got the whole text back."

You need to show us exactly what output you got. I ran your code without any problems. Here's the (cut-down) output I got:

New Paragraph: Assembly and Manufacturing **************************************** New Paragraph: The Company's assembly and manufacturing operation +s include PCB assembly and the manufacture of subsystems and complete products. Its PCB assem +bly ... Company has invested in FICO, a producer of injection molded plastic f +or Asia electronics companies with facilities in Shenzhen, China. **************************************** New Paragraph: As OEMs seek to provide greater functionality in s +maller products, they increasingly require advanced manufacturing technologies and processes +. Most of ... performance. The Company also provides traditional PTH electronics ass +embly using PCBs and leaded components for lower cost products.; ****************************************
"I am quite confused with these errors."

You don't show any errors. I added this to the start of your code:

use strict; use warnings; use autodie;

No errors or warnings were emitted.

— Ken

Replies are listed 'Best First'.
Re^2: Extract Paragraph From Text
by perlbeginneraaa (Novice) on Sep 08, 2015 at 15:18 UTC
    Hi Ken, Thanks much for the reply! I expect exactly what you got here, that is, the code prints the paragraphs separately. However, when I execute the perl codes on my computer, it returns the whole text back. By errors, I mean the output is different from what I expected. Sorry for the confusion. Best Regards

      In the code posted, the record separator ($/) is set to "". The paragraphs are separated by a blank line, i.e. a line containing no characters between its start and end. The code works as posted.

      If you are using the same code and your result is a single block of text, then the paragraphs are not separated by empty lines, on your test setup.

      Are you testing with the exact code you posted here? Or is the input data in a file, and you copied it into your code to post here? What happens if you copy and run the code from your OP, using copy/paste from the raw ("download") link?

      Bottom line: the text you are processing must have its paragraphs separated by something other than a blank line.

      The way forward always starts with a minimal test.
        Hi 1nickt,

        Thanks for your reply! What I am really trying to do is to parse a batch of text files. In each file, I want to get each paragraph separately and do analysis within each paragraph. The code posted here is just an example.

        When I copy and paste the text from the original text file and execute the code, I got the whole text back instead of each separately.

        If the text I have does not separate paragraphs by blank lines, can I still get separately paragraphs?

        Best Regards
      ... when I execute the perl codes on my computer, it returns the whole text back.

      What Perl codes? Not because I doubted kcott but just to be able to say I did so, I copied out the code of the OP and only added the use statements kcott did, and I got the same output without warnings or errors. Are you saying that you can do the same and get a different output? If so, how does it differ? (It's not enough just to say "It's not what I want.")

      Offhand, I cannot think of any environmental variable or OS peculiarity that would cause the originally posted code to differ in its behavior. We need more info about this.


      Give a man a fish:  <%-{-{-{-<