in reply to matching paragraphs

If you have a look at perlvar, in the $/ section, you have this:

$/ The input record separator, newline by default. This influences Perl's idea of what a "line" is. Works like awk's RS variable, including treating empty lines as a terminator if set to the null string. (An empty line cannot contain any spaces or tabs.) You may set it to a multi-character string to match a multi-character terminator, or to "undef" to read through the end of file. Set- ting it to "\n\n" means something slightly differ- ent than setting to "", if the file contains con- secutive empty lines. Setting to "" will treat two or more consecutive empty lines as a single empty line. Setting to "\n\n" will blindly assume that the next input character belongs to the next paragraph, even if it's a newline.

So, if you set $/="" you'll get something similar to your needs (if I'm understanding what you need).

I would also recommend you to localize $/, so it will return to the previous value. So, you can surroud your paragraphs with tags with something like that:

#!/usr/bin/perl my @paras = do { local $/=""; <DATA> }; #Idiom localizing $/ for (@paras){ print "<html paragraph>\n"; print $_; print "</html paragraph>\n"; } __DATA__ This is the first paragraph of text. This is the second. This is also the second. This is the 3rd.

P.D.: You should read Writeup Formatting Tips, please put code tags around your code.

Replies are listed 'Best First'.
Re^2: matching paragraphs
by imhotep (Novice) on Apr 18, 2005 at 20:13 UTC
    Thanks for your reply! How do I get the input in? The file is in the same folder and thus far I have only learnt how to work from standard input using a while loop e.g.
    while ($line =<>); etc.
    I cannot see where the input is coming from? Apologies for my lack of understanding!!!!
      • <> is an abreviation for < ARGV> and takes a filespec from the command line.
      • < STDIN> reads from STDIO,
        open FH, /dev/null or die "Can't open file\n"; while <FH> { #your code }
        This code sais: open the file /dev/null and give me a filehandle FH as a reference. Then read from the file referenced by FH and put that into $_. Stop reading at the end of the file.

        Reread post and ammended:

        imhotep wrote:

        #The variable, input in this case being #standard input

        That is wrong. You opened input.txt and created a filehandle INPUT. You are reading from input.txt.


        s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}