plenn08
plenn08

Reputation: 163

Perl: Extract data from different sections of a text file simultaneously

I want to extract data from different sections of a text file simultaneously. Is it possible to open the file using two separate filehandles(as shown below) ? Or is it possible to cache the location of the first file handle and then return to that point in the document when I close the second one?

Note: I am only reading data from the text file, never writing to it.

open( $filehandle, '<:encoding(UTF-8)', $filename )
    or die "Could not open file '$filename' $!";
$row = <$filehandle>;
{
    replace_unicode_char();
    if ( $row =~ /$table_num/ ) {
        open( $filehandle_reg, '<:encoding(UTF-8)', $filename )
            or die "Could not open file '$filename' $!";
        $line = <$filehandle_reg>;
        if ( $line =~ /Section\_[0-9]+/ ) {
            # Do something...
        }
    }
}

Upvotes: 0

Views: 102

Answers (1)

Richard Neish
Richard Neish

Reputation: 8806

You can use the seek() function to move around in the file, and the tell() function to get the current position in the file.

So, instead of having two filehandles, have two variables storing a position in the file, and use seek() to jump back and forth between them.

Upvotes: 2

Related Questions