in reply to Initializing Hashes of Hashes (of Hashes)
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %form = ( a=>5,); print Dumper (\%form); print get_key(\%form,'a.b.c'),"\n" ; # Doesn't vivify print Dumper (\%form); getset_key(\%form,'a.b.c', 55); # Set $form{a}{b}{c} to 55 print Dumper (\%form); getset_key(\%form,'a.a.c.d', "umkay"); #Set $form{a}{a}{c}{d} to umkay print Dumper (\%form); getset_key(\%form,'a.b.c.d', 15); #Set $form{a}{b}{c}{d} to 15, bye c= +55! print Dumper (\%form); getset_key(\%form,'a.b.1', 100); # Sets $form{a}{b}[1] = 100 print Dumper (\%form); getset_key(\%form,'a.b.11111111', 10); # Array too big, bails print Dumper (\%form); BEGIN { my $MAX_ARRAY_INDEX = 100; sub get_key { # Does not vivify non existant keys my $h = shift; my $k = shift; die "You need to pass a reference to get_key!" unless ref($h); die "No key specified!" unless defined $k; my @p = split(/\./,$k); die "An index is too big!: ".join(', ',@p) if grep { $_=~m/^\d+ +$/ && $_ > $MAX_ARRAY_INDEX} @p; my $v = get_avhv($h,@p); } sub getset_key { # Does vivify non existant keys my $h = shift; my $k = shift; my $v = shift; die "You need to pass a reference to get_key!" unless ref($h); die "No key specified!" unless defined $k; my @p = split(/\./,$k); die "An index is too big!: ".join(', ',@p) if grep { $_=~m/^\d+$ +/ and $_ > $MAX_ARRAY_INDEX} @p; vivify_avhv($h,@p,$v); } sub vivify_avhv { my $h = \shift; my $v = pop; local $_; while (@_){ if ($_[0]=~/^\d+$/){ $$h = [] unless UNIVERSAL::isa( $$h, "ARRAY" ); $h = \$$h->[shift ()]; }else{ $$h = { } unless UNIVERSAL::isa( $$h, "HASH" ); $h = \$$h->{shift()}; } } $$h = $v; return $h; } ################################################# sub get_avhv { my $h = \shift; local $_; while (@_){ if ($_[0]=~/^\d+$/){ return unless UNIVERSAL::isa( $$h, "ARRAY" ); return if $_[0] >= @{$$h} ; # Prevent autoviv $h = \$$h->[shift ()]; }else{ return unless UNIVERSAL::isa( $$h, "HASH" ); return if ! exists $$h->{$_[0]}; # Prevent autoviv $h = \$$h->{shift()}; } } return $$h; } }
|
|---|