use strict; use GDBM_File ; my %hash; tie %hash, 'GDBM_File', 'words.dbm', &GDBM_WRCREAT, 0640; my $dict_file = '/usr/share/dict/longlist'; open DICT, "<$dict_file" or die "open($dict_file): $!"; my @words = (); my $prefix = ''; while() { chomp; $_ = lc($_); if(substr($_, 0, 3) ne $prefix) { if(@words) { $hash{$prefix} = join(' ', @words); @words = (); } $prefix = substr($_, 0, 3); } push @words, $_; } untie %hash ; #### use strict; use Benchmark; use GDBM_File ; my $dbm_file = 'words.dbm'; my %hash; $/ = undef; my $data = ; my @words = map {lc($_)} ($data =~ /(\w+)/g); print scalar(@words), "\n"; timethis(1, 'one_tie()'); timethis(1, 'many_ties()'); sub one_tie { my($found, $not_found) = (0, 0); tie %hash, 'GDBM_File', $dbm_file, &GDBM_WRCREAT, 0640; foreach (@words) { my $prefix = substr($_, 0, 3); if(exists($hash{$prefix}) and $hash{$prefix} =~ /\b$_\b/) { $found++; } else { $not_found++; } } untie %hash ; print "Found: $found\nNot found: $not_found\n"; } sub many_ties { my($found, $not_found) = (0, 0); foreach (@words) { tie %hash, 'GDBM_File', $dbm_file, &GDBM_WRCREAT, 0640; my $prefix = substr($_, 0, 3); if(exists($hash{$prefix}) and $hash{$prefix} =~ /\b$_\b/) { $found++; } else { $not_found++; } untie %hash ; } print "Found: $found\nNot found: $not_found\n"; } __DATA__ insert output of 'perldoc perltoot' here :-)