You seem to be doing a lot of extra work.

$cost = $h->{$location}{$building};

If that doesn't work for you, create a little class to make the hash an object and give it some accessors.

As for your new method of doing things, it's been done before and never really caught on. It's not easy to do either. In your example,

$cost = $h->{"Locations.$location.Buildings.$b.cost"};

You need to restrict the values of $location and $b so they don't contain the separator character. Not only do you have to program it, but you have to document it and tell users they can't use it. Or, let users escape that character, but then your parsing is more difficult. There's a lot of ways for this to go wrong. There is a reason Perl doesn't do this anymore. Your way is the Perl4 way of creating multi-dimensional hashes: see the documentation of $; in perlvar (it tells you not to do this).

The way to make the conventional syntax more comfortable is to use it more.

--
brian d foy <bdfoy@cpan.org>

In reply to Re: Dotted hash access by brian_d_foy
in thread Dotted hash access by sfink

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.