Your MGF file seems to be divided into records (blocks). If the first lines of a record are always the same information in the same order, you can skip the useless lines. For that, you should read the file record by record instead of line by line (which would be better than your method for finding pairs, if for any reason the TITLE line doesn't match for one record, you'll match the RT of a record with the TITLE of the next, and so on until the next error). This can be done using the input record separator, or $/. If your records are paragraph (separated by at least one empty line, but contain no empty lines themselves) you can do something like:

use constant { TITLE => 3, RT => 7 }; { # extra block so that $/ retrieve its previous value after reading t +his file local $/ = ""; # Paragraph mode while (my $record= <MGF>) { my @lines = split "\n", $record; $TI = "$1" if $lines[TITLE] =~ /^TITLE=(.*)/; $RT = "$1" if $lines[RT] =~ /^RTINSECONDS=(.*)/; } }

If the records are not paragraphs, this could probably work by setting $/ = "SEARCH" if all records start with it.

If some of the fields in the records may be missing, and you can't use their position, reading record by record can still help you skip the lines at the end once you have found what you are looking for:

{ local $/ = ""; # Paragraph mode RECORD: while (my $record = <MGF>) { my @lines = split "\n", $record ; $TI = undef; $RT = undef; for my $line (@lines) { $TI = "$1" if $line =~ /^TITLE=(.*)/; $RT = "$1" if $line =~ /^RTINSECONDS=(.*)/; next RECORD if defined $TI and defined $RT; # skip to next recor +d } } }

In any case, /^TITLE=(.*)/ is a little faster than /TITLE=(.*)/ because it tells perl to stop searching if TITLE isn't at the beginning of the line.

Edit : added missing end of code sections


In reply to Re: Script far too slow with large files - wisdom needed! by Eily
in thread Script far too slow with large files - wisdom needed! by biologistatsea

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.