Tie::ListKeyedHash

Tie::ListKeyedHash is a system allowing the use of anonymous arrays as keys to a hash.
Download

Tie::ListKeyedHash Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Benjamin Franz
  • Publisher web site:
  • http://www.nihongo.org/snowhare/utilities/ftpweblog/

Tie::ListKeyedHash Tags


Tie::ListKeyedHash Description

Tie::ListKeyedHash is a system allowing the use of anonymous arrays as keys to a hash. Tie::ListKeyedHash is a system allowing the use of anonymous arrays as keys to a hash.SYNOPSIS use Tie::ListKeyedHash; tie %hash, 'Tie::ListKeyedHash'; my $live_key = ; $hash{$live_key} = 'Hello!'; $hash{} = 'Goodbye!'; print $hash{},"n"; delete $hash{$live_key}; my @list = keys %{$hash{}}; print "@listn"; untie %hash ;Alternatively keys are accessible as: $hash{'key','items','live'} = 'Hello!';(a bare list/array for the key rather than using an anon list/array reference).But that slows down the accesses by around 10% and cannot be used for keys that conflict with the value of the $; special variable.Also usable via the object interface methods 'put', 'get','exists','delete','clear'. The object interface is about 2x as fast as the tied interface.Tie::ListKeyedHash ties a hash so that you can use a reference to an array as the key of the hash. It otherwise behaves exactly like a normal hash (including all caveats about trying to use a key as both a hash reference and a scalar value).This frees you from needing to 'hardwire' hash references in code or having to write tree traversal code to reach arbitrary points in a hash tree.Example: ######################## #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Tie::ListKeyedHash; my %example; tie (%example, 'Tie::ListKeyedHash'); %example = ( 'a' => { 'b0' => { 'c' => 'value of c', 'd' => 'value of d', 'e' => { 'f' => 'value of f', }, }, 'b1' => { 'g' => 'value of g', }, }, 'h' => 'r', ); my $b_key = ; my $d_key = ; my $d = $example{$d_key}; print "d = $dn"; my $e_key = ; my $e = $example{$e_key}; print 'e = ' . Dumper ($e); my $f_key = ; my $f = $example{$f_key}; print "f = $fn"; my $h_key = ; my $h = $example{$h_key}; print "h = $hn"; ########################The virtues of this particular way of accessing hash-of-hashes (HoH) vs bare hardwired derefererences or 'tree crawling' are as follows:1) As the number of levels in a HoH increases, the tied object asymptotically approaches the speed of hardwired hash dereferencing without the loss of flexibility penalty of having to hardwire the keys into code in advance.This gives an important property that it gets faster the deeper a HoH becomes as compared with the speed of software driven tree traveral.So you can build and access arbitrarily structured HoH and still access deeply buried elements in the tree quickly.2) The format was designed to use memory efficiently. It takes only a few hundred extra bytes over the size of an untied HoH in memory or when serialized (via Data::Dumper or Storable for example) regardless of how deep the hash is.3) A reference to an existing HoH can be passed into Tie::ListKeyedHash->new and all of the OO key lists access methods will "just work". Example: use Tie::ListKeyedHash; my %hash = ( 'a' => { 'b' => 'c' } ); my $obj = Tie::ListKeyedHash->new(%hash); my $b_value = $obj->get(); Requirements: · Perl


Tie::ListKeyedHash Related Software