in reply to Reading from large files
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Reading from large files
by zer (Deacon) on Mar 21, 2006 at 04:07 UTC | |
by GrandFather (Saint) on Mar 21, 2006 at 04:22 UTC |