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

DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Reading from large files
by zer (Deacon) on Mar 21, 2006 at 04:07 UTC
    Thanks Grandfather however this didnt work either. It produced the same results.

      Use a very small sample of your data that reproduces the problem in place of the data I gave. Show us what you see printed and what you expect to be printed. File size is not the issue.

      We need to see the code that you are using that fails and the data that it fails with. If you can't reduce the pertinent code to a size similar to my sample and the pertinent data to a similar size, you do not understand your own code and data and have not identified the actual problem.


      DWIM is Perl's answer to Gödel