[Kc] return vs newline

JYOUNG79 at kc.rr.com JYOUNG79 at kc.rr.com
Thu Dec 7 09:31:41 PST 2006


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.

Thanks for your help.  I really appreciate it.

Jay

On Thu, Dec 07, 2006 at 10:40:20AM -0600, JYOUNG79 at kc.rr.com wrote:
>> Hi,
>> 
>> I'm trying to loop thru paragraphs in a file but evidently the file 
has 
>> carriage returns instead of newlines so when perl reads it in, it 
reads 
>> it in as one big line instead of separate paragraphs.  Does anyone 
know 
>> if there's a way 
to have perl use carriage returns instead so I can 
>> loop thru each paragraph one at a time?

>Hi Jay -
>
>I'm not sure I actually caught the difference between what you're
>considering "lines" versus "paragraphs", but here's my best shot:
>
>You can read about what affects this behavior under the
>"$INPUT_RECORD_SEPARATOR" heading in the perlvar perldoc. Also, check
>out the "Newlines" bit in perlport.
>
>But, I'm guessing that there's something else more simple going on
>here. Could you post a small sample of your code that fails to do what
>you expect, please?
>
>-Andy





More information about the kc mailing list