Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Adding value to hash

by danidin (Novice)
on Jul 25, 2005 at 12:44 UTC ( [id://477789]=perlquestion: print w/replies, xml ) Need Help??

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

Hi All,
Here is my code, pretty simple:

my %temp_hash; my ($temp_name, $temp_id) = split ("~", $sql_result); $temp_hash{$temp_name} = $temp_id;

For this code I get this error message :

Global symbol "$temp_hash" requires explicit package name at /exlibris +/sfx_ver/sfx_version_3/dz_3/sfxadmin/dataloader_active.cgi line 46. Global symbol "$temp_hash" requires explicit package name at /exlibris +/sfx_ver/sfx_version_3/dz_3/sfxadmin/dataloader_active.cgi line 47. Execution of /exlibris/sfx_ver/sfx_version_3/dz_3/sfxadmin/dataloader_ +active.cgi aborted due to compilation errors. Premature end of script headers: dataloader_active.cgi

What might be the problem ?

Thanks,
Daniel.

20050725 Cleaned up by Corion: Added formatting

Replies are listed 'Best First'.
Re: Adding value to hash
by davorg (Chancellor) on Jul 25, 2005 at 13:04 UTC

    You're not showing us the correct piece of your code. The error message mentions a scalar called $temp_hash, but you're not using that variable in this piece of code.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Adding value to hash
by Fletch (Bishop) on Jul 25, 2005 at 12:53 UTC

    Hrmm, that doesn't look like 47 lines of code. You're not showing enough to say for sure, but my guess is that the code you have shown is inside the BLOCK of a while or for loop, and then you're getting the errors in subsequent code outside the loop where %temp_hash is no longer in scope.

    You need to consult Coping with Scoping (google for mjd perl coping with scoping; it's too early in the morning for linking).

    Update: Oh, yeah I didn't even catch the scalar part of the error messsage (ENOCAFFEINE). Possibly you're trying to use $temp_hash as a hashref (e.g. $temp_hash->{$foo} or ${$temp_hash}{$foo}) later, again outside the scope in which %temp_hash was declared.

    --
    We're looking for people in ATL

Re: Adding value to hash
by reasonablekeith (Deacon) on Jul 25, 2005 at 12:57 UTC
    I guess you're using $temp_hash as a scalar somewhere, but think you're using as a hash...

    The error is produced because you've probably got.

    use strict;
    at the top of your script (which is good). This is forcing you to declare your variables with "my".

    Now you HAVE declared the HASH %temp_hash, but you HAVEN'T declared the SCALAR $temp_hash.

    The lines you posted look fine, the error must be on another line, or you've made changes to your example.

    Updated: to slightly less fractured english

    ---
    my name's not Keith, and I'm not reasonable.
Re: Adding value to hash
by bofh_of_oz (Hermit) on Jul 25, 2005 at 13:13 UTC
    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...

      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";
      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.
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://477789]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-18 03:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found