I'm working on lexing mathematical expressions and I'm constructing a toy example of a non-deterministic finite automaton.

I've decided that the transition tables should be stored in hashes so that input characters can actually serve as keys to the hash. It is possible that some inputs may map to more than one transition, so I've also decided that each key (input character) should have a reference to an anonymous array as its value. If the input character only specifies one transition, then the value should be a reference to an array containing only one element!

This way of thinking produces some states with pretty boring transition tables. For example, a state that represents the situation of "about to read an identifier," which I've called 'id_0', might have a transition table which looks like the following.

id_0 => {a=>['id_1'], b=>['id_1'], . . . z=>['id_1']}

Now, in my toy example, I've constructed this state using the following technique.

use strict; use warnings; my @array = ('a'..'z'); my %hash = map {$_ => ['id_1']} @array; my $hashref = \%hash; my %states = (id_0 => $hashref); print("The first transition for (state,input) pair (id_0,x) is\n"); print($states{id_0}->{x}->[0]);

My question is, "Is there a way to do this without the intermediate %hash variable?"

Any help will be appreciated, whether it's a "Here's how you do it" one liner, or a "Why do you want to it that way, when you could do it this way" type of thing.

~T


In reply to 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.