Q: detect non-existent hash entries w/function?

Discussion in 'Perl Misc' started by Daniel Friedman, Jul 1, 2003.

  1. I'm trying to figure out how to to detect non-existent hash entries
    via a function, something of this general nature:


    function hashexists($$) {
    # first arg might have to be non-scalar to get this to work,
    # that would be OK, too
    my var1 = $_[0]; # maybe a scalar, maybe not
    my $err_hint = $_[1];

    if (exists <something here related to var1>) {
    # If I can get this function to work, maybe I'll redefine
    # it to provide the value of the specified hash,
    # or die otherwise; for now, let's just...
    return(1);
    } else {
    die "<something [else?] related to var1> doesn't exist; (hint:
    $err_hint)"
    return(0);
    }


    I haven't been able to do this sort of thing
    with "use strict", which I regard as a must;
    Any thoughts on how to achieve this?

    Thanks very much,
    --daniel
     
    Daniel Friedman, Jul 1, 2003
    #1
    1. Advertising

  2. Daniel Friedman <> wrote:

    > my var1 = $_[0]; # maybe a scalar, maybe not



    No "maybe not" about it.

    If it starts with a dollar sign, it *is* a scalar.

    So what did you really mean there?


    > I haven't been able to do this sort of thing



    I can't figure out what "sort of thing" you are after. Sorry.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 1, 2003
    #2
    1. Advertising

  3. On 30 Jun 2003 18:03:42 -0700,
    Daniel Friedman <> wrote:
    > I'm trying to figure out how to to detect non-existent hash entries
    > via a function, something of this general nature:


    $ perldoc -f exists

    > function hashexists($$) {
    > # first arg might have to be non-scalar to get this to work,
    > # that would be OK, too


    The first argument will be a scalar. That's what that prototype does
    for you. The first and second argument will be forced to be
    interpreted in scalar context.

    So,

    sub foo($$) { print "$_[0]:$_[1]\n" }

    my @a = (1, 2);
    my @b = (1, 2, 3, 4);

    foo(@a, @b);

    Will print

    2:4

    > my var1 = $_[0]; # maybe a scalar, maybe not


    No. A syntax error.

    If you meant to write

    my $var1 = $_[0];

    then yes, it is a scalar. Always.

    > my $err_hint = $_[1];
    >
    > if (exists <something here related to var1>) {


    What does "something here related to var1" mean?

    > # If I can get this function to work, maybe I'll redefine
    > # it to provide the value of the specified hash,
    > # or die otherwise; for now, let's just...
    > return(1);
    > } else {
    > die "<something [else?] related to var1> doesn't exist; (hint:
    > $err_hint)"


    Similar question about this bit.

    > return(0);


    This return will never be reached. You just called die.

    > }


    I have tried, but I don't really know how to interpret this.

    > I haven't been able to do this sort of thing
    > with "use strict", which I regard as a must;


    Well... I don't think strict has anything to do with it.

    > Any thoughts on how to achieve this?


    What is 'this'? Could you be a bit more specific? I am really not sure
    what you're trying to do.

    Martien
    --
    |
    Martien Verbruggen | We are born naked, wet and hungry. Then
    Trading Post Australia | things get worse.
    |
     
    Martien Verbruggen, Jul 1, 2003
    #3
  4. (Tad McClellan) writes:

    > Daniel Friedman <> wrote:
    >
    > > my var1 = $_[0]; # maybe a scalar, maybe not

    >
    >
    > No "maybe not" about it.
    >
    > If it starts with a dollar sign, it *is* a scalar.


    Yes, this is so.

    This is why I find the following annoying

    my $glob;
    my $r = \$glob;
    print ref $r; # prints SCALAR
    $glob = *FOO;
    print ref $r; # prints GLOB

    --
    \\ ( )
    . _\\__[oo
    .__/ \\ /\@
    . l___\\
    # ll l\\
    ###LL LL\\
     
    Brian McCauley, Jul 1, 2003
    #4
  5. (Daniel Friedman) writes:

    > I'm trying to figure out how to to detect non-existent hash entries
    > via a function, something of this general nature:


    If I understand what you're trying to do, why not just use

    exists $hash{$key}

    instead of creating a messy, complicated function to do the same thing?

    Or, if you wanted to check multiple keys simply,

    @nonexistent = grep { not exists $hash{$_} } @possible_keys;

    Charlton
     
    Charlton Wilbur, Jul 1, 2003
    #5
  6. There was apparently some ambiguity in my earlier post, so let me try
    to clarify.

    1) Purpose/motivation: I'm writing a script which will have lots of
    places in which I want to carefully check for the existence of a hash
    entry, and also avoid autovivification (I admit that the
    non-autovification piece was not in my original post, I omitted it to
    try to focus my question), before getting the value from the
    hash--here's what I mean:

    $errhint = __FILE__; # this is just an example
    unless (exists $myhash{$foo}) {
    die "$errhint: myhash{$foo} does not exist";
    }
    unless (exists $myhash{$foo}{$bar}) {
    die "$errhint: myhash{$foo}{$bar} does not exist";
    }
    unless (exists $myhash{$foo}{$bar}{$baz}) {
    die "$errhint: myhash{$foo}{$bar}{$baz} does not exist";
    }
    # apparently it exists, so let's get the value:
    $thevalue = $myhash{$foo}{$bar}{$baz}

    ---> I want to make a function which does this. The function's
    arguments
    should be the "specification" of a hash whose existence is to be
    checked, and some string for helping find the error ($errhint, above).
    (The function would preferably be able to handle a non-fixed number
    of hash levels.)

    2) When I wrote in my earlier post "maybe a scalar, maybe not", I
    meant that I wasn't fussy if the function receives the specification
    as a single scalar argument (e.g. "myhash{$foo}{$bar}{$baz}") or in
    some other form ("myhash", "$foo", "$bar", "$baz"). I think it can't
    be a reference since I don't imagine it's possible to make a reference
    to something which doesn't necessarily exist.

    I hope that clarifies my question. Again, thanks very much,
    --daniel
     
    Daniel Friedman, Jul 1, 2003
    #6
  7. Daniel Friedman

    Uri Guttman Guest

    >>>>> "DF" == Daniel Friedman <> writes:

    DF> There was apparently some ambiguity in my earlier post, so let me try
    DF> to clarify.

    DF> 1) Purpose/motivation: I'm writing a script which will have lots of
    DF> places in which I want to carefully check for the existence of a hash
    DF> entry, and also avoid autovivification (I admit that the
    DF> non-autovification piece was not in my original post, I omitted it to
    DF> try to focus my question), before getting the value from the
    DF> hash--here's what I mean:

    first level hash lookups never autovivify. only multilevel lookups do
    that. basically autoviv happens when you dereference through undef. see
    my paper on this at:

    http://tlc.perlarchive.com/articles/perl/ug0002.shtml

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jul 1, 2003
    #7
  8. Daniel Friedman <> wrote:

    > There was apparently some ambiguity in my earlier post, so let me try
    > to clarify.
    >
    > 1) Purpose/motivation: I'm writing a script which will have lots of
    > places in which I want to carefully check for the existence of a hash
    > entry, and also avoid autovivification (I admit that the
    > non-autovification piece was not in my original post, I omitted it to
    > try to focus my question),




    Errr, but that *is* your question:

    detect non-existent hash entries w/o autoviv'ing

    The answer to your OP questions was:

    exists $myhash{$foo}{$bar}{$baz}

    which doesn't avoid the autoviving for you.


    > ---> I want to make a function which does this. The function's
    > arguments
    > should be the "specification" of a hash whose existence is to be
    > checked, and some string for helping find the error ($errhint, above).
    > (The function would preferably be able to handle a non-fixed number
    > of hash levels.)
    >
    > 2) When I wrote in my earlier post "maybe a scalar, maybe not", I
    > meant that I wasn't fussy if the function receives the specification
    > as a single scalar argument (e.g. "myhash{$foo}{$bar}{$baz}") or in
    > some other form ("myhash", "$foo", "$bar", "$baz").


    > I hope that clarifies my question.



    It doesn't clarify your question, it states your question. :)

    Here is a function to get you started, it wants a hash-ref as
    the first arg, followed by a list of keys, eg:

    do_something() if exists_no_viv(\%myhash, $foo, $bar, $baz)


    ---------------------------
    sub exists_no_viv { # "walk" down a chain of hashrefs
    my $h = shift; # a ref to hash, start at the "top"

    while ( my $k = shift ) {
    return 0 unless exists $h->{$k}; # fail w/o autoviv
    if ( ref $h->{$k} eq 'HASH' ) # go one level "deeper"
    { $h = $h->{$k} }
    }
    return 1;
    }
    ---------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 1, 2003
    #8
  9. Tad,

    Late yesterday, the same idea sprouted in my mind, and lo, you've
    given me the code! Bingo, this is just what I was looking for; thanks
    very much.

    --dan


    (Tad McClellan) wrote in message news:<>...

    > Here is a function to get you started, it wants a hash-ref as
    > the first arg, followed by a list of keys, eg:
    >
    > do_something() if exists_no_viv(\%myhash, $foo, $bar, $baz)
    >
    >
    > ---------------------------
    > sub exists_no_viv { # "walk" down a chain of hashrefs
    > my $h = shift; # a ref to hash, start at the "top"
    >
    > while ( my $k = shift ) {
    > return 0 unless exists $h->{$k}; # fail w/o autoviv
    > if ( ref $h->{$k} eq 'HASH' ) # go one level "deeper"
    > { $h = $h->{$k} }
    > }
    > return 1;
    > }
    > ---------------------------
     
    Daniel Friedman, Jul 2, 2003
    #9
  10. Daniel Friedman

    Uri Guttman Guest

    >>>>> "DF" == Daniel Friedman <> writes:

    <don't top post>

    DF> Late yesterday, the same idea sprouted in my mind, and lo, you've
    DF> given me the code! Bingo, this is just what I was looking for;
    DF> thanks very much.

    again, i will point you to this article which has similar code.

    http://tlc.perlarchive.com/articles/perl/ug0002.shtml

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jul 2, 2003
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul W

    Non-existent datagrid?

    Paul W, Nov 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    332
    Paul W
    Nov 10, 2004
  2. ElGordo
    Replies:
    2
    Views:
    6,134
    Ersin Gençtürk
    Apr 29, 2005
  3. Joel D. Kraft

    Non-existent page is not throwing 404 error

    Joel D. Kraft, May 24, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    1,482
    Joel D. Kraft
    May 25, 2005
  4. dkode
    Replies:
    4
    Views:
    768
    dkode
    Apr 12, 2006
  5. Les Caudle
    Replies:
    1
    Views:
    346
    Cowboy \(Gregory A. Beamer\)
    Sep 19, 2006
Loading...

Share This Page