<?xml version="1.0" encoding="windows-1252"?>
<node id="451848" title="Re: Heisenberg Uncertainty Hash" created="2005-04-27 00:30:11" updated="2005-08-15 10:45:46">
<type id="11">
note</type>
<author id="300037">
Roy Johnson</author>
<data>
<field name="doctext">
Here's a demo of a sub you might find useful. It descends the hash chain checking existence at each level. It returns &amp;#91;a reference to, to ensure logical truth] the value if everything exists.
&lt;code&gt;
#!perl
use warnings;
use strict;

sub safe_exists {
    my ($href, @list_of_descent) = @_;
    use List::Util 'first';
    if (first {
            not (exists $href-&gt;{$_} and $href=$href-&gt;{$_})
        } @list_of_descent) {
        return ();
    }
    else {
        return \$href;
    }
}

my %hash = ('a' =&gt; {
                      'value' =&gt; 1,
                      'foo' =&gt; 'bar',
                   },
            'b' =&gt; {
                      'value' =&gt; 2,
                      'foo' =&gt; 'bar',
                   },
            'c' =&gt; {
                      'value' =&gt; 3,
                      'foo' =&gt; 'bar',
                   },
           );

for (qw(a b c d)) {
    if (my $href = safe_exists(\%hash, $_, 'value')) {
        print "$_ value is $$href\n";
    }
    else {
        print "Key $_ doesn't exist. Valid keys are: ";
        print join (", ", keys %hash), "\n";
    }
}&lt;/code&gt;
&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-300037"&gt;
&lt;hr&gt;
&lt;small&gt;&lt;b&gt;Caution:&lt;/b&gt; Contents may have been coded under pressure.&lt;/small&gt;
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
451791</field>
<field name="parent_node">
451791</field>
</data>
</node>
