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


in reply to Adding value to hash

Looking at your code, I do not see an error. $temp_hash{$temp_name} is a valid key/value pair of a properly declared hash...

From my own mistakes in the past, here's a suggestion: check the output of your SQL query. It looks like your $sql_result is not defined and so $temp_hash becomes a scalar, which is not defined. Insert a

print "sql_result=($sql_result), temp_name=($temp_name), temp_id=($tem +p_id)\n";
That would help clarify what's going on with the code...

Alternatively, assign $sql_result a value directly for testing and verify the behaviour of the program...

 

UPDATE: tests have shown that in the situation I've described, perl does not complain... it only does if %temp_hash is really not defined. Therefore, I tend to agree with Fletch: it might be the scope...

--------------------------------
An idea is not responsible for the people who believe in it...

Replies are listed 'Best First'.
Re^2: Adding value to hash
by wazoox (Prior) on Jul 25, 2005 at 14:09 UTC
    It looks like your $sql_result is not defined and so $temp_hash becomes a scalar, which is not defined.

    Certainly not. $temp_hash{undef} is entirely distinct from $temp_hash.

    use strict; my %t; my $t; my $k; $t{$k}='toto'; $t='titi'; # print 'toto', not 'titi' print $t{$k}, "\n";
Re^2: Adding value to hash
by reasonablekeith (Deacon) on Jul 25, 2005 at 13:20 UTC
    quote:
      It looks like your $sql_result is not defined and so $temp_hash becomes a scalar

    What! I'm off to learn java if that's true.

    ---
    my name's not Keith, and I'm not reasonable.