Rewrite to use while instead of foreach. I can't tell if this sample is meaningful or not, but may give you the idea (note local data is a good technique for sample code like this):
use strict; use warnings; my %DB; my $store = "<Head>"; my $head; while (<DATA>) { if (/.*<B>/ && ! /Last modified/){ #If line is a title my $temp = $_; $_=~ s/(<[^<>]+>)+//g; chomp; if (/\(/){ $store = $_; $DB{$store} = $temp; }else{ $store = $_; $DB{$store} = $temp; } }else{ #Not a title ($store ne "<Head>")? $DB{$store}:$head .= $_; } } print join "\n", map {"$_ -> $DB{$_}"} keys %DB; __DATA__ Title line<B> a line another line <Head><B> this line that line
Prints:
use strict; use warnings; my %DB; my $store = "<Head>"; my $head; while (<DATA>) { if (/.*<B>/ && ! /Last modified/){ #If line is a title my $temp = $_; $_=~ s/(<[^<>]+>)+//g; chomp; if (/\(/){ $store = $_; $DB{$store} = $temp; }else{ $store = $_; $DB{$store} = $temp; } }else{ #Not a title ($store ne "<Head>")? $DB{$store}:$head .= $_; } } print join "\n", map {"$_ -> $DB{$_}"} keys %DB; __DATA__ Title line<B> a line another line <Head><B> this line that line
In reply to Re: Reading from large files
by GrandFather
in thread Reading from large files
by zer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |