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?


s//----->\t/;$~="JAPH";s//\r<$~~/;{s|~$~-|-~$~|||s |-$~~|$~~-|||s,<$~~,<~$~,,s,~$~>,$~~>,, $|=1,select$,,$,,$,,1e-1;print;redo}

In reply to Re: quickly create reference to boring hash. by starbolin
in thread quickly create reference to boring hash. by tford

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.