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

Hi Monks ! I'm about to go crazy! i need help, i am codeing a small perl program but got a problem. This is sample text.txt file --
Beginning Perl is a different kind of Perl book. It's written particul +arly with the beginning programmer in mind, but it doesn't treat you like an idiot, and experienced programmers wi +ll not feel patronized. It covers a lot of ground, from the very basics of programming, right +through to developing CGI applications for the web. More importantly, it emphasizes good Perl practice, and r +eadable and maintainable code.
This is sample perl script code
open F, "<text.txt"; while (<F>){ print "Result : $1 " if($_ =~ m/particularly(.*)/); }
runing and result
Result : with the beginning programmer in mind,
but there is was a problem. i am writing this result in dos:
with the beginning programmer in mind, but it doesn't treat you like an idiot, and experienced programmers wi +ll not feel patronized. It covers a lot of ground, from the very basics of programming, right +through to developing CGI applications for the web. More importantly, it emphasizes good Perl practice, and r +eadable and maintainable code.
How do I do this?

Replies are listed 'Best First'.
Re: Find strings
by moritz (Cardinal) on Oct 01, 2010 at 08:11 UTC
    It seems that on DOS, the line endings are not recognized, thus the whole content ends up as a single logical line in your program.

    Since I don't have DOS available I can't test my suggestion, but after reading Perlio I guess a binmode F, ":raw";

    After the line with the open should help.

    By the way you should always check if system calls like open() actually succeed:

    open my $f, '<', 'text.txt' or die "can't open file 'text.txt' for reading: $!"; binmode $f, ':raw'; while (<$f>) { print "Result: $1" if /particularly(.*)/; }
    Perl 6 - links to (nearly) everything that is Perl 6.
      Sorry, i know little english. It would be nice to have a small sample. This example is important to me. Thank you moritz for reply
        I've now included the binmode call in my previous reply.
Re: Find strings
by raybies (Chaplain) on Oct 01, 2010 at 17:18 UTC

    Clearly your regular expression just slurped the whole end of the file, rather than finding an end of line character.

    In dos the default end of line character (which is actually two characters) is "\r\n", so it may be that the dos perl expects both of those characters to end the file. If you took your text file from linux and transferred it using a binary copy of some form, then your text file would still be in linux format, and would either need to be converted to dos (in standard linux you can do a dos2unix and unix2dos commands to convert between the two... at least I can on my redhat system...)

    You can also change the end of line character that perl's searching for... okay so I'm calling it the wrong thing, it's really called the $INPUT_RECORD_SEPARATOR, or is called $/ in perl. Instead of it being set to \r\n, just set it to \n.

    The other possibility is that there's a default in the regex. If that's the case, anchor your regex.

    Instead of looking for m/particularly(.*)/ look for m/particularly(.*)$/

    The $ says "go to the end of line" ($/ character sequence). It's a good practice to anchor your regexes if you want to garantee you're going to the end of a line, rather than slurping the whole file.

    Hope that helps,

    --Ray

      thank you very much all. I'm trying this method raybies, but i can't writing all lines
        So did you set:
        $/ = "\n";
        prior to doing the regex search?
Re: Find strings
by rwburden (Novice) on Oct 01, 2010 at 15:44 UTC
    What version of Perl are you using, and what kind of line breaks does the input file text.txt contain (Unix, Mac or other)? I have perl 5.10.1 on Windows XP (ActivePerl distribution), and it has no problem recognizing Unix line breaks, but I have not tested it with Mac line breaks).
      Os Type: windows 7 ultimate Perl Version : v5.12.2 i am open the bin type file and remove the "new line" characters and writing new another file. but i could not, Was 3 days