[tpm] Populating an unfixed number of hash elements
Shaun Fryer
sfryer at sourcery.ca
Fri May 9 09:58:49 PDT 2008
Hey Madi,
This works. I had to modify how the sub was called slightly, but hopefully it's
still workable for you.
################################################################################
use strict;
use warnings;
use Data::Dumper;
my $string1="some::string::of::elements";
my $string2="some::string";
my $string3="some::string::of::other::elements";
my $string4="another::set::of::keys";
my $href={};
$href = mk_href($string1, "a value");
print Dumper $href;
$href = mk_href($string2, 12);
print Dumper $href;
$href = mk_href($string3, "a really long string that takes up pages");
print Dumper $href;
$href = mk_href($string4, "something else again.");
print Dumper $href;
sub mk_href {
my ($keystr, $value) = @_;
my @keys = split /::/, $keystr;
my $last_key = pop @keys;
my $href = {};
$href->{$last_key} = $value;
while (my $key = pop @keys) {
my $elem = {};
$elem->{$key} = $href;
$href = $elem;
}
return $href;
}
################################################################################
Cheers,
--
Shaun Fryer
On Fri, May 09, 2008 at 11:43:55AM -0400, Madison Kelly wrote:
> I am not sure that the subject makes a lot of sense, sorry. :)
>
> In a few places I take a string that can have an arbitrary number of
> elements in it that I use as hash keys. I split up this string and feed
> in a passed value to a hash reference using the given hash elements.
>
> So far, I've not found an effective way to do this, so I've resorted to
> a frighteningly dumb solution "that works". However, it's not at all
> flexible and frankly, I am ashamed of it. So please, TPM, help me regain
> some dignity! :)
>
> I need a way to /intelligently/ create a hash entry. I've tried
> 'eval'ing it into existence but that never seems to work for some reason...
>
> Thanks!!
>
> Madi
>
> -=-=-=-=-=-=-=-
> Here is an example of what I am trying to do:
>
> my %hash=();
>
> $string1="some::string::of::elements";
> $string2="some::string";
> $string3="some::string::of::other::elements";
> $string4="another::set::of::keys";
>
> $self->_to_hash(\%hash, $string1, "a value");
> $self->_to_hash(\%hash, $string2, 12);
> $self->_to_hash(\%hash, $string3, "a really long string that takes up
> pages");
> $self->_to_hash(\%hash, $string4, "something else again.");
>
> sub _to_hash
> {
> my $self=shift;
> my $hashref=shift;
> my $keystring=shift;
> my $value=shift;
>
> my @key=split /::/, $hashref;
>
> # Here is where it gets... undignified...
> if (@key==5)
> {
> $$hashref{$key[0]}{$key[1]}{$key[2]}{$key[3]}{$key[4]}=$value;
> }
> elsif (@key==4)
> {
> $$hashref{$key[0]}{$key[1]}{$key[2]}{$key[3]}=$value;
> }
> elsif (@key==3)
> {
> $$hashref{$key[0]}{$key[1]}{$key[2]}=$value;
> }
> elsif (@key==2)
> {
> $$hashref{$key[0]}{$key[1]}=$value;
> }
> elsif (@key==1)
> {
> $$hashref{$key[0]}=$value;
> }
> else
> {
> croak "Unsupported number of kash keys.\n";
> }
>
> return();
> }
> _______________________________________________
> 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