[Kc] return vs newline

djgoku djgoku at gmail.com
Thu Dec 7 10:24:56 PST 2006


On 12/7/06, JYOUNG79 at kc.rr.com <JYOUNG79 at kc.rr.com> wrote:
> Hi Andy,
>
> Thanks for your reply.  The code is simply looking through a text file
> for whatever the user wants to search for and then it counts how many
> time it appears in the file.  I've commented out some lines below so I
> could test and see what was happening.
>
> ----------------------------
> #!/usr/bin/perl -w
>
> print "Please enter text to search for: ";
>
> chomp($in = <STDIN>);
>
> $n = 0;
>
> while (<>) {
>         chomp;
>
>         #if ($_ =~ /$in/i) { $n++ }
>         print "$in = $_\n";
> }
>
> #print "\nTotal count for $in: $n\n";
> ----------------------------
>
> Now that I'm looking at the results closer I'm not sure what's going
> on.  Originally I thought the file might have carriage returns in it
> instead of newlines which would make perl read the whole thing in a
> while loop instead of separating out each paragraph.  Anyway if I run
> the above code through the shell with a text file that just contains:
>
> ===============================================
> Wednesday, December 6, 2006 4:58:05 AM
> =========================
> ======================
> Job File: 12-06_05;57;45 AM_test.txt
> --
> Name: Jay Young
> Dept: PSD
>
>
> then I get this in my shell window (which is totally wrong):
>
> Please enter month and day to search: hi
> Dept: PSD Young_05;57;45 AM_test.txt================
> I'll mess around somemore with the code when I return from lunch.  I'm
> still curious though, it's my understanding that when using a while
> loop to loop thru each paragraph from a file with Perl that you must
> have the 'newline' character in the file otherwise it won't work.  Is
> there a way to get around this?  I'll also look at the perldoc info you
> gave me.

http://perldoc.perl.org/perlfaq4.html#How-can-I-count-the-number-of-occurrences-of-a-substring-within-a-string%3f

This may be of interest also.


More information about the kc mailing list