[tpm] Breaking a string up into hash keys

Quantum Mechanic quantum.mechanic.1964 at gmail.com
Sat Apr 13 08:33:08 PDT 2013


Just hope the OP doesn't exceed the recursion limit ;)

Now we just need someone to post a loop version, and we'll be done.

Cheers,
Shaun

On Apr 13, 2013, at 4:11 PM, Richard Dice <rdice at pobox.com> wrote:

> My solution is to go recursive...
> 
> richard-dice-s-computer-9:Desktop rdice$ cat ./hashkeys.pl
> #!/usr/bin/env perl
> 
> use warnings;
> use strict;
> 
> use Data::Dumper qw ( Dumper );
> 
> my $conf;
> 
> $conf->{foo}{bar} = "a";
> $conf->{baz} = "1";
> $conf->{this}{and}{the}{other}{thing} = "what?";
> 
> my @joinkeys = qw (
> foo::bar
> baz
> this::and::the::other::thing
> );
> 
> foreach my $joinkey ( @joinkeys ) {
>   my @bits = split /::/, $joinkey;
>   print hashval($conf, \@bits);
>   print "\n";
> }
> 
> exit 0;
> 
> sub hashval {
>   my ($hash, $keybits) = @_;
>   if ( ref($hash->{$keybits->[0]}) eq 'HASH' ) {
>     return hashval($hash->{$keybits->[0]}, [ @$keybits[1..$#$keybits]]);
>   } elsif ( ! ref($hash->{$keybits->[0]}) ) {
>     return $hash->{$keybits->[0]};
>   } else {
>     die "Foo! They key string you gave me doesn't correspond to a known proper hash-of-hash entry within the 'conf' structure --> "  . Dumper($hash, $keybits);
>   }
> }
> richard-dice-s-computer-9:Desktop rdice$  ./hashkeys.pl
> a
> 1
> what?
> 
> 
> 
> On Sat, Apr 13, 2013 at 1:29 AM, Digimer <lists at alteeve.ca> wrote:
> Hi all,
> 
>   I've got a rather large, random-depth hash. ie:
> 
> $conf->{foo}{bar} = "a";
> $conf->{baz} = "1";
> $conf->{this}{and}{the}{other}{thing} = "what?";
> 
>   And so on. The number of hash keys can be quite varied, depending on the use.
> 
>   So now I want to be able to take a string that is in the format:
> 
> foo::bar
> baz
> this::and::the::other::thing
> 
>   Split on the :: and use that to pull the value out of the hash.
> 
> Any help would be much appreciated! :)
> 
> -- 
> Digimer
> Papers and Projects: https://alteeve.ca/w/
> What if the cure for cancer is trapped in the mind of a person without access to education?
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
> 
> _______________________________________________
> toronto-pm mailing list
> toronto-pm at pm.org
> http://mail.pm.org/mailman/listinfo/toronto-pm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/toronto-pm/attachments/20130413/14eba510/attachment-0001.html>


More information about the toronto-pm mailing list