[Kc] return vs newline

Andrew Moore amoore at mooresystems.com
Thu Dec 7 12:30:42 PST 2006


On Thu, Dec 07, 2006 at 11:31:41AM -0600, 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.

Hi Jay - 

I see a few problems here, and I'll point out the ones that jump to
mind first.

It looks like there's a problem printing out your output. I suspect
that some lines are running over others, probaby because of a
end-of-line problem. Perl is supposed to be smart about this and use
"\n" to smartly use what's most correct for your OS type. (see perldoc
perlport.) But, it may not be doing something right, and it looks like
many of your lines of output are being written on top of each other.

I've attached a copy of your script that I have rewritten a little
bit. See if it works better for you. Some things I changed (not all of
which matter) are:

* use strict, this helps me catch some of my simple errors

* used longer variable names. I couldn't differentiate $n and $in very
  well.

* printed out the string I was searching for so that I could see it
  better

* used 'warn' instead of print so that it would give me line numbers
  and handle my end-of-line problems better

* put your file in a __DATA__ section. This actually may mask your
  end-of-line problem since the data is now in the script itself and
  considering that I wrote my copy on linux. I'm hopeful that when you
  expand this example to use an external file that you can handle
  that.


Some things that could still be improved:

* the /$searchfor/i regex should not be redone each time. we should
  make use qr() to make a stored regex that we refer to in the loop
  foir efficiency reasons.

* we should probably untaint $searchfor

* this doesn't count multiple occurrences in each line

* do we really need to chomp the lines?

Let me know if this helps you out or if you still have questions.

-Andy



-------------- next part --------------
A non-text attachment was scrubbed...
Name: jay.pl
Type: text/x-perl
Size: 615 bytes
Desc: not available
Url : http://mail.pm.org/pipermail/kc/attachments/20061207/e03ebe4f/attachment.bin 


More information about the kc mailing list