in reply to Script far too slow with large files - wisdom needed!
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
|
|---|