Philip Morley wrote:
"$Bill Luebkert" <[EMAIL PROTECTED]> To: Philip Morley <[EMAIL PROTECTED]> cc: [EMAIL PROTECTED] 05/11/02 12:31 Subject: Re: Evaluating multi-dimensional hashes






Philip Morley wrote:

$VAR1 = {
         'level1' => {
                       'level2' => {
                                       'level3' => {}
                                   }
                     }
       }


The above is the contents of a multi-dimensional hash I have produced
from

parsing an XML String.  (The Data::Dumper formatting has been adjusted
for

brevity).

What I want to do is check whether level3 is "null" (i.e. contains no
value

or sub-hashes), but I can't work out a way of expressing this.

I have the reference to the hash, named $XMLRef, and when printing
something like:

print "$$XMLRef{level1}{level2}{level3}";

I get a HASHREF printed.

How can I evaluate if $$XMLRef{level1}{level2}{level3} is null?

Maybe something like this:

my %hash = (
             'level1' => {
                         'level2' => {
                                     'level3' => { }
                         }
             }
);
my $XMLRef = \%hash;
print scalar keys %{$XMLRef->{level1}{level2}{level3}}, "\n";

__END__

Thanks fo that.

One extra question -

print scalar keys %{$XMLRef->{level1}{level2}{level4}}, "\n";

will print 0 with the above example.  How can I tell if a subkey exists in
a hash (level4 doesn't in this example)?
Untested:

if (exists $XMLRef->{level1}{level2}{level4}) {


--
  ,-/-  __      _  _         $Bill Luebkert   ICQ=162126130
 (_/   /  )    // //       DBE Collectibles   Mailto:dbe@;todbe.com
  / ) /--<  o // //      http://dbecoll.tripod.com/ (Free site for Perl)
-/-' /___/_<_</_</_     Castle of Medieval Myth & Magic http://www.todbe.com/

_______________________________________________
ActivePerl mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to