http://www.perlmonks.org?node_id=860442

kalyanrajsista has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

I'm confused of how to use the references in conditional statements like

#!/usr/bin/perl use strict; my $hash_ref = &getHashRef(); # Check if hash reference has really values in it.... if ( $hash_ref ) { .....# do something }

I want to do the processing only if there is data inside the $hash_ref. eventhough there is no data inside the hashref, it is going into the loop, which it should not be.

I'm trying the following ways. ..

# Which one is correct ..... if ( %$hash_ref ) { ...... } # OR if ( $hash_ref ) { ........ }

Also please provide me check for existence of values inside Array references.....

Replies are listed 'Best First'.
Re: Check for existance & definedness for Hash and Array references
by ikegami (Patriarch) on Sep 17, 2010 at 07:18 UTC

    When dealing with references, it's often possible for the reference to be undefined. If that's possible here, you want to make sure you got a hash ref, and that the reference hash has some elements.

    if ($hash_ref && %$hash_ref)

    On the other hand, if you know getHashRef never returns undef, then you don't need to check that.

    if (%$hash_ref)
Re: Check for existance & definedness for Hash and Array references
by moritz (Cardinal) on Sep 17, 2010 at 08:01 UTC
    Also please provide me check for existence of values inside Array references.....

    It works just like with hash references, but with a @ instead of %. See also: ikegami's reply, perlreftut, perlref, perldsc.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Check for existance & definedness for Hash and Array references
by planetscape (Chancellor) on Sep 17, 2010 at 08:26 UTC
Re: Check for existance & definedness for Hash and Array references
by BrowserUk (Patriarch) on Sep 17, 2010 at 06:43 UTC
    I'm trying the following ways. ..

    One of which you've already dismissed as not working. So what happens when you try the second way?

    Also, you might consider fixing getHashRef() so that it doesn't return references to empty hashes.

    What have you tried for arrays?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Check for existance & definedness for Hash and Array references
by TomDLux (Vicar) on Sep 17, 2010 at 16:12 UTC

    If you are confused about what's in a variable at a certain point, the best solution is to run the script in the debugger, and examine the variables.

    perl -d myscript.pl

    while start Perl, load and compile the script, and wait for you to type a command. Try h as your first command, it will print a summary of available commands. l to list sections of your script; n to execute a line of code; s to step into a subroutine; r to continue until the current subroutine returns; x to display the value of a variable ... note that you need to backslash true arrays and hashes, to examine a reference to it, to properly see what's happening:

    DB<1> $h1; DB<2> $h2 = {} DB<3> $h3 = {has => 'value'} DB<4> x $h1 0 undef DB<5> x $h2 0 HASH(0x8406f78) empty hash DB<6> x $h3 0 HASH(0x8406d08) 'has' => 'value'

    h1 is a scalar with no value; h2 is a scalar storing a reference to a hash, but the hash contains no data; h3 is a scalar containing a reference to a hash which has a tag/value pair ... but note that both key and value can be true or false in a boolean sense, and they can both even be undef.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.