in reply to Re: Hashes -- Core dump
in thread Hashes -- Core dump

Hi nuffin, Thanks much for an almost instant reply. I used NBDM_File and tie and tried the following snippet of code. It still cores when it reaches foreach loop (to sort and print the hash to a file)If i take the foreach loop out, it works fine. I need your help again...I want to sort the hash in ascending order of numeric key and print to a file. Thank you!!!
use NBDM_File; tie(%h,NDBM_File,'test_tie.tmp',O_RDWR/O_CREAT, 0640); while (<INPUT>) { $id = substr($_, 9, 11); if (! exists($h{$id}) ) { $h{$id} = $_; } else { .... .... } } foreach (sort keys %h) { print OUTPUT $h{$_}; } untie %h;

Replies are listed 'Best First'.
Re: Re: Re: Hashes -- Core dump
by nothingmuch (Priest) on Oct 23, 2002 at 16:51 UTC
    When you sort keys you are loading an array of keys to the memory, and sorting it, which can take up a lot again. I have the most perfect solution for you...
    use strict; use warnings; use DB_File; my $num_order_btree = new DB_File::BTREEINFO; $num_order_btree->{compare} = sub { $_[0] <=> $_[1] }; tie my %h, 'DB_File', 'test_tie.tmp', O_RDWR|O_CREAT, 0640, $num_order +_btree; while (<INPUT>){ $id = substr($_,9,11); unless (exists($h{$id})){ $h{$id} = $_; } else { } } while (defined ($_ = each %h)){ print OUTPUT $h{$_}; } untie %h;


    -nuffin
    zz zZ Z Z #!perl