Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Reading entire file.

by stuffy (Monk)
on May 04, 2001 at 09:58 UTC ( [id://77880]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, it's me again, and I'm still working on the same script that I have posted about previously. I am to the point where everything is working, except my script is only reading the first 200-205 lines of my 1800 line text file. My question is simple. When using :
while (<FOO_FILE>){ if (/$foo/){ $that = 1; } if (/$bar/){ $that = 0; } if ($that){ print $_; } }
The file contains a list of equipment, and comments that someone has put in about the equipment for the day. I am sorting all comments by equipment number...eg.

Apr 23 2001 0630
Tool01 - ran good
Tool02 - replaced inner bearing

Apr 24 2001 2330

Tool01 - Broke spindle temp transducer
waitin on replacement
Tool02 - Did not run today
I have it set up to start printing when it sees the first tool I am looking for, and stops when it sees the next one. It does this fine, except it stops in the middle of the file. I set up a counter to see what line it goes to, and it is usually line 204. This is a different point in the file depending on what date I put in for the start. In other words, it stops on about the same line every time, but the content of that line is different almost every time. I am not able to post the actual file due to the nature of my work, but I quess I actually have two questions. Is ther an easier way to test and print between two points in a file, and how can I make sure that it will scan the entire file each time. Stuffy Welcome to the life of the perpetually confused.

Replies are listed 'Best First'.
(kudra) Re: Reading entire file.
by kudra (Vicar) on May 04, 2001 at 10:06 UTC
    Update: Oops, thank you cLive--I misread the $that = 0; as another $that = 1 and therefore saw it as just checking and printing single lines.

    As for your problem with stopping, I'm not seeing any reason why this section wouldn't work (assuming $foo and $bar are what you want them to be)

    Ps. You can print $. instead of keeping a counter.

      The lines will not contain both $foo and $bar at the same time. As a very newbie I do not understand exactly what your code is saying. (my programming experiance comes from pascal programming in high school 6 years ago.) Also, how does Perl know that it is at the end of a file?

      Stuffy
        See eof:
        if ( eof FILEHANDLE ) { print "Reached end of file.\n"; }


        ar0n ]

      This doesn't do what (s)he wants. They're looking at grabbing a block starting with $foo and up to, but not including, $bar.

      (as far as I can see).

      cLive ;-)

        you are correct, I do not want to include $bar. I wouldn't mind if it started after (not including) $foo either.
        Stuffy
        (is a he)
Re: Reading entire file.
by bjelli (Pilgrim) on May 04, 2001 at 12:13 UTC

    I'll just latch onto one of your questions:

    >Is there an easier way to test and print between two points in a file,

    Yes, there is. I tried to explain it in from...to

    In your case, I'd recommend you break out of the loop after you're finished with the tool:

    while (<FOO_FILE>){ if (/$foo/){ $that = 1; } if (/$bar/){ last; # emergency exit from the loop ;-) } if ($that){ print $_; } }

    You could also seperate it into two loops, one for searching for the tool, one for reading the lines for that tool.

    while (<FOO_FILE>){ last if /$foo/; # ignore all lines until we find foo } while (<FOO_FILE>){ last if /$bar/; print $_; # do something with the lines we find now } close FOO_FILE; # just ignore the rest of the file
    --
    Brigitte    'I never met a chocolate I didnt like'    Jellinek
    http://www.horus.com/~bjelli/         http://perlwelt.horus.at
Re: Reading entire file.
by diarmuid (Beadle) on May 04, 2001 at 13:52 UTC
    Well if you want to check if you are between two point in a file then use the ... operator. eg
    if(/$foo/.../$bar/){ }
    evaluates to true if the current line is between the two regular expressions.
Re: Reading entire file.
by cLive ;-) (Prior) on May 04, 2001 at 10:23 UTC
    "I am not able to post the actual file due to the nature of my work.."

    So, why not make up a few lines of fake data to give us some idea of structure. And tell us what you are putting in $this and $that?

    My money's on a dodgy regexp :)

    cLive ;-)

      I do have some sample text in my origional post. That is simular to what can be found in my text file, except there are 26 of one type of tool, 20 of another type, and 10 of another. I am only conserned with the first 26. My actual code is:

      while (<PD_FILE>){ if (/$tool_number/) $continue = 1; } if (/$end_tool/) $continue = 0; } if ($continue){ print $_; } }


      the value for $tool_number is Tool01 which will and does give $end_tool a value of Tool02. Everything that I have checked up to this point seems to work except the loop. I tried some code that was posted as a reply to another question.
      while (<^(*?)$tool_number(.*)$/) {


      that did not seem to work either.

      stuffy
        Hmmm, maybe this will make sense? (adding the braces you missed (typo :)
        my $tool_number = 1; # (say - perhaps you're looping through them, hen +ce... $tool_number = sprintf(%02d,$tool_number); # add leading zero, if need +ed while (<PD_FILE>){ if (/^Tool$tool_number/) { $continue = 1; } if (/^Tool/ && !(/^Tool$tool_number/)) { $continue = 0; } if ($continue){ print $_; } }

        FYI - ^ matches the beginning of a string (line in this case).

        cLive ;-)

        Yes, but is the sample text output or input. If input, what do you want output? ditto vice versa...

        cLive ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://77880]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-03-29 08:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found