Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

File search question

by lvanhout (Curate)
on Jun 25, 2004 at 14:32 UTC ( #369628=perlquestion: print w/ replies, xml ) Need Help??
lvanhout has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I am reading a file looking for a specific flagged line. Sample 1 will loop through the entire file never activating the IF() statement. I fixed with sample 2 which stops reading the file at line 4 "testpoint\n". I do not understand why sample 1 doesn't work. I tried several variations including using chomp() . Am I missing something simple like a white space char?
sample 1: while(<FILE>) { if($_ eq "testpoint\n") { last; } } sample 2: while(<FILE>) { if(/testpoint/) { last; } } File contents being read: <begin file> This is a test file data at the front of file more test data testpoint data after the test Last line <end of file>

*note: fixed typo eg => eq happened while simplifying example
also fixed the ? => " happened when cutting a pasting from open office

Comment on File search question
Download Code
Replies are listed 'Best First'.
Re: File search question
by neilwatson (Curate) on Jun 25, 2004 at 14:48 UTC
    You have eg instead of eq. Also, would using an regex not be a better solution? if (m/testpoint\n/){ ...

    Neil Watson

Re: File search question
by davido (Archbishop) on Jun 25, 2004 at 15:22 UTC

    Using 'eg' where you mean 'eq' is the first problem.

    The second problem is that you're looking for exactly "testpoint\n", but in your question you told us that the line you want to stop on is "?testpoint?\n" (I assumed the \n since you're not chomping). Notice the '?' symbols? 'eq' tests for equality. Equality is not just the existance of certain text within a string, but the existance thereof to the exclusion of anything else.

    Later in your post, you show example text without the '?' symbols. Which is it? You do have to be specific when you're using 'eq'. There could also be trailing whitespace not visually apparent after 'testpoint', which could be throwing off 'eq'.


Re: File search question
by graff (Chancellor) on Jun 26, 2004 at 04:08 UTC
    If the behavior is as you describe with the typos fixed, then the problem may have to do with line-termination issues. Maybe line 4 of the file actually contains "testpoint\r\n" (i.e. a CRLF) instead of just "\n". If the perl script is running on the same OS that created the text file, then this ouqht to work:
    while (<FILE>) { chomp; if ( $_ eq "testpoint" ) { last; } }
    But if the text file was made on one OS and the script is running under a different OS (and if it's not gauranteed that the file has not gone through a dos2unix or unix2dos filter), then you're better off using the regex approach. To get effectively the same behevior as a working "eq", just anchor the beginning and ending of the regex:
    while (<>) { if ( /^testpoint\s*$/ ) { last; } }
Re: File search question
by lvanhout (Curate) on Jun 26, 2004 at 04:26 UTC
    I have embraced the regex approach. Thanks for the input.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://369628]
Approved by Paladin
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2015-11-30 00:28 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (754 votes), past polls