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

Hello Monks, I have not written any perl in ages and I was recently asked to reverse engineer several scripts at work. Everything was going well until I got to this.

if ( exists $nums{$stuff[0]} ){...}

My question is what is $nums{$stuff[0]} ? I know that I am looking at a key ($stuff) of a hash (%nums). The 0 completely throws me off. Does the zero imply that there is more than one $stuff ? Is 0 the first of a series of keys with the same name? Any help would be appreciated. -Newb

Replies are listed 'Best First'.
Re: Newbie Question
by toolic (Bishop) on Feb 17, 2009 at 20:26 UTC
      thank you were right.
Re: Newbie Question
by lostjimmy (Chaplain) on Feb 17, 2009 at 20:29 UTC

    This line is checking if the key $stuff[0] exists in the hash %nums.

    $stuff[0] is the first element in the array @stuff.

    Example:

    my @stuff = qw/a b c d/; my %nums = ( a => 1, b => 2, c => 3, ); if (exists $nums{$stuff[0]}) { print "$stuff[0] is in \%nums"; ) }
    This will print a is in %nums.

      Thank you you were correct
Re: Newbie Question
by pgaraitonandia (Novice) on Feb 17, 2009 at 20:41 UTC
    There is a an array with the name @stuff. That makes a lot more sense. Thank you both for the reply
Re: Newbie Question
by Anonymous Monk on Feb 18, 2009 at 02:41 UTC
    Hi,

    Perhaps the following might help?


    @stuff = ( "myfirstvar" );
    %nums = ( 'myfirstvar', 1 );
    if ( exists $nums{ $stuff[0] } ) { print "howdy!"; }


    It appears that 'stuff' is an array. You're dereferencing
    the first element of that array and then deteferencing
    a hash element based on what's stored in the array's
    element. ie: Suppose that "foo" is stored in the first
    element of the array 'stuff', then you're deteferencing
    the element with the index 'foo' in the 'nums' hash
    table. You are essentially checking, if that index in
    that hashtable exists. If it does, then do ...

    Ozy
      <nitpick>

      I believe you are misusing the term "dereference". I think it would be more accurate to use the term "subscript". Refer to Subscripts and perlreftut for the distinction.