Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Check for existance & definedness for Hash and Array references

by kalyanrajsista (Scribe)
on Sep 17, 2010 at 06:18 UTC ( #860442=perlquestion: print w/ replies, xml ) Need Help??
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.....

Comment on Check for existance & definedness for Hash and Array references
Select or Download Code
Re: Check for existance & definedness for Hash and Array references
by BrowserUk (Pope) 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 ikegami (Pope) 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 (Canon) on Sep 17, 2010 at 08:26 UTC
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://860442]
Approved by Corion
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2014-12-28 14:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (181 votes), past polls