in reply to quick question about hash element access

How come everyone else gets to have the fun? Let's see if you'll let me in with this little hack that lets you play with all sorts of mixtures of structures...hashes, and arrays, and anonymous subs...OH MY! And in case something goes haywire and the key you specify doesn't exist [:'(] have no fear, it just returns undef and allows you to go along on your merry way! Follow the yellow br...err...I mean...umm...here's the code:

#!/usr/bin/perl require 5.6.1; use strict; my $hash; $hash = { a => { b=> [ \\\$hash, { c => sub { $_ = shift; return [ join "","j",$_,"h" ]; } } ] } }; sub retrievekey { my $endvalue = shift; my $keys = shift; my @keys = split /\./, $keys; foreach my $x (@keys) { return undef unless defined $endvalue; $endvalue = $$endvalue while (ref($endvalue) eq "REF"); eval { if (ref($endvalue) eq "HASH") { $endvalue = $endvalue->{$x}; } elsif (ref($endvalue) eq "ARRAY") { $endvalue = $endvalue->[$x]; } elsif (ref($endvalue) eq "CODE") { $endvalue = $endvalue->($x); } else { return undef; } }; return undef if ($@); } $endvalue; } print retrievekey($hash, 'a.b.0.a.b.1.c.ap.0'),$/; __DATA__ output: japh

Hey, it derefs any references to references and follows paths for hashes, arrays, and subs. This is just a silly bit of fun. Of course, don't use this or whoever maintains the code will hunt you down and shoot you... and nobody wants that... at least I don't :-P

antirice    
The first rule of Perl club is - use Perl
The
ith rule of Perl club is - follow rule i - 1 for i > 1