in reply to Dotted hash access
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Dotted hash access
by sfink (Deacon) on Nov 25, 2004 at 09:40 UTC | |
by brian_d_foy (Abbot) on Nov 25, 2004 at 19:24 UTC |