Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to look at the next line of a file in Perl

I have a piece of code which opens up a file and parses it. This text document has a redundant structure and has multiple entries. I need to peek ahead within my loop to see if there is a new entry, if there is, I will be able to parse all of the data my program extracts. Let me first show my implementation so far

use strict;
my $doc = open(my $fileHandler, "<", "test.txt");

while(my $line = <$fileHandler>) {
    ## right here I want to look at the next line to see if 
    ## $line =~ m/>/ where > denotes a new entry
}
like image 768
user1876508 Avatar asked Dec 08 '22 18:12

user1876508


1 Answers

Try handling the iteration yourself:

my $line = <$fileHandler>;
while(1) { # keep looping until I say so
    my $nextLine = <$fileHandler>;

    if ($line =~ m/>/ || !defined $nextLine) {
        ### Do the stuff
    }
    ### Do any other stuff;

    last unless defined $nextLine;
    $line = $nextLine;
}

I added the extra check in the if statement under the assumption that you will also want to process what you have when you reach the end of the file.

Alternatively, as suggested by friedo, if the file can fit into memory, you can load the whole thing into an array at once:

my @lines = <$fileHandler>;
for (my $i = 0; $i <= $#lines; $i++) {
    if ($i == $#lines || $lines[$i+1] =~ />/) {
        ### Do the stuff
    }
}

This is more flexible in that you can access any arbitrary line of the file, in any order, but as mentioned the file does have to be small enough to fit into memory.

like image 172
reo katoa Avatar answered Dec 11 '22 10:12

reo katoa