in reply to tying a hash from a big dictionary
use Data::Dumper; use Search::Dict::Object; my $d = Search::Dict::Object->new( file => "/tmp/dict.txt", keyval_xfrm => sub { split /\t/ }, comp => sub { $_[0] cmp $_[1] }, # should correspond to file sort +order ); print Dumper { aaa => $d->get('aaa'), foo => $d->get('foo'), bar => $d->get('bar'), baz => $d->get('baz'), zzz => $d->get('zzz'), }; __END__ $VAR1 = { 'bar' => '789', 'baz' => '456', 'aaa' => undef, 'foo' => '123', 'zzz' => undef };
$ cat /tmp/dict.txt aho 234 bar 789 bat 567 baz 456 cut 678 foo 123 yyy 000
package Search::Dict::Object; use warnings; use strict; use Search::Dict (); sub new { my $class = shift; my $self = bless { }, $class; $self->_init(@_); return $self; } sub _init { my $self = shift; %{ $self } = @_; unless (defined($self->{FH})) { my $file = $self->{file} || "<unspecified file>"; open($self->{FH}, "<", $file) or die("Cannot open: $file", "\n ", $!); } } sub get { my ($self, $key) = @_; return undef unless defined($key); my $FH = $self->{FH}; my $comp = $self->{comp} || sub { $_[0] cmp $_[1] }; my $keyval_xfrm = $self->{keyval_xfrm} || sub { $_ => $_ }; my $opts = { comp => $comp, $self->{keyval_xfrm} ? (xfrm => sub { chomp($_[0]); (map $keyval_xfrm->(), $_[0]) +[0] }) : (), }; if (Search::Dict::look($FH, $key, $opts) != -1) { my $entry = <$FH>; return undef unless defined($entry); chomp($entry); my ($k, $v) = map $keyval_xfrm->(), $entry; return $v if $comp->($k, $key) == 0; } return undef; } sub DESTROY { my $self = shift; close($self->{FH}) if $self->{FH}; } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: tying a hash from a big dictionary
by BrowserUk (Patriarch) on Nov 01, 2011 at 09:39 UTC | |
by repellent (Priest) on Nov 02, 2011 at 01:24 UTC |