A hash would not appear to be the ideal data structure to contain your transition tables. A hash contains a lot of magic behind the scenes to insure that each key maps to exactly one container in the internal structure. A precaution which your application expressly does not need. A hash also has to work over a wide population of keys and insure that the population of keys is distributed evenly over the internal storage. Your, admittedly simple, example has a fixed and small key range of a..z which could be stored sequentially.
It would seem from your example code that needs would be served by a simple array with a symbolic index. Perl supplies something called a pseudo-hash though I don't know how efficient it is. The module Hash::Util allows a similar functionality with "locked" hashes.
#!/usr/bin/perl # this code build a table of array-refs keyed to alphabetic # characters. The arrays could be used to hold state transistions. require 5.8.8; use strict; use fields; use Data::Dumper; my $inputs = fields::phash([('a'..'z')], [ map { [1,1] } ('a'..'z')]); $inputs->{c} = [ 0, 1 ]; $inputs->{f} = [ 2, 1 ]; $inputs->{g} = [ 0, 1 ]; print Dumper map { $inputs->{$_} } ( 'a'..'g' ); __END__ Troll [127] as maint ~/ %perl src/symarray.pl + [6:40pm] $VAR1 = [ 1, 1 ]; $VAR2 = [ 1, 1 ]; $VAR3 = [ 0, 1 ]; $VAR4 = [ 1, 1 ]; $VAR5 = [ 1, 1 ]; $VAR6 = [ 2, 1 ]; $VAR7 = [ 0, 1 ];
I'm not sure if this is as efficient as I would like. Question for perlmonks; Does Perl actually create an array here or otherwise optimize or do I still suffer from the overhead of hash lookups?
In reply to Re: quickly create reference to boring hash.
by starbolin
in thread quickly create reference to boring hash.
by tford
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |