[tpm] Breaking a string up into hash keys

Rob Janes janes.rob at gmail.com
Sun Apr 14 09:44:48 PDT 2013

It's not a deep hash though.  it's just composing a single key by
joining with an obscure character that you're unlikely to find in a
key normally.

Sure, it should work fine, and will probably be a lot speedier than
lookups by loop or recursion.  On the other hand, if there's a lot in
the hash, this method could result in a lot of hash collisions and
inefficient lookups.  And, on the other other hand, to see that slow
things down so much that this multidimensional thing will be slower
than loop/recursion ...that would be a corner case.

On Sun, Apr 14, 2013 at 11:43 AM, Uri Guttman <uri at stemsystems.com> wrote:
> On 04/14/2013 08:53 AM, Olaf Alders wrote:
>> use Hash::Flatten qw( flatten );
> you don't need that module if you just use a very obscure but valid perl
> trick call multidimensional hashes. this is from old perl4 days but still
> works. if you use a list as the key in a scalar hash lookup, the list is
> joined with $; (see perlvar) and used as a single key. $; defaults to "\034"
> which is not printable. note that that value is unlikely to be in any
> printable key so the joined string will be unique and work as a flat hash
> key.
> so all you need to do is use that trick to generate the single level hash
> and also to access it.
>> my $key = 'this::and::the::other::thing';
>> $key =~ s/::/./g;
> instead of s/// just do a split on ::
>         my @keys = split /::/, $keys ;
>         $flat{ @keys } = 'foo' ;
> if that doesn't work because the array is put into scalar context, you can
> do the join directly:
>         $flat{ join $;, @keys } = 'foo' ;
> and stay away from string eval for any normal data manipulation. it is slow
> and dangerous and actually rarely needed. it is NOT needed here at all.
> uri
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm

More information about the toronto-pm mailing list