Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re-using a hash value in same hash?

by oakley (Scribe)
on Mar 06, 2001 at 00:09 UTC ( [id://62329]=perlquestion: print w/replies, xml ) Need Help??

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

Not sure if I am out of my mind by trying or if I just cant grasp what I am trying to do, but here goes.

What I am trying to do involves a hash of hashes and subroutines. I am running the program with -w and use strict (learned that one a long time ago :) It's better explained after a snippet:
my %HoH = ( yadda1 => { key1 => "value1", key2 => &GetValue, # returns date string key3 => &Compare($var1,$HoH{yadda1}{key2}), }, yadda2 => { key1 => "value1", key2 => &GetValue, # returns date string key3 => &Compare($var1,$HoH{yadda2}{key2}), }, );
The problem exists when I try to get the value from key2 into the subroutine being called *IN* key3. It tells me "Global symbol "%HoH" requires explicit package name ... ". The hash has been declared, but I am basically calling it from within itself - is this not possible? If it is, what am I doing wrong?

- oakley
Embracing insanity - one twitch at a time >:)

Replies are listed 'Best First'.
Re: Re-using a hash value in same hash?
by merlyn (Sage) on Mar 06, 2001 at 00:15 UTC
    The hash %HoH is not declared until the end of the statement in which it appears. That lets you do things like:
    my $this = $this * 2; # use outer $this to initialize local inner $thi +s
    But it's getting in your way here. You'll need to set up the hash as a series of actions, not as an initialization.

    -- Randal L. Schwartz, Perl hacker

Re: Re-using a hash value in same hash?
by chipmunk (Parson) on Mar 06, 2001 at 00:24 UTC
    When you declare a variable with my, the declaration takes effect for the following statement. This allows you to do things like:
    #!perl my $x = 'outer'; { my $x = $x; # new $x, copy value from old $x print "$x\n"; $x = 'inner'; print "$x\n"; } print "$x\n"; __END__
    In your code, you need to declare %HoH before you reference it. For example:
    my %HoH; %HoH = ( yadda1 => { key1 => "value1", key2 => &GetValue, # returns date string key3 => &Compare($var1,$HoH{yadda1}{key2}), }, yadda2 => { key1 => "value1", key2 => &GetValue, # returns date string key3 => &Compare($var1,$HoH{yadda2}{key2}), }, );
    That will avoid the warning from strict, but it doesn't actually solve the problem. When you assign to a variable, if you refer to that same variable on the RHS of the assignment, you will get the old value of the variable. This is a lot like what happens with my:
    my %HoH = ( key1 => 'first' ); %HoH = ( key1 => 'second', key2 => $HoH{key1}, );
    $HoH{key2} will be 'first', because that was the value of $HoH{key1} when the RHS of the assignment was evaluated.

    I would suggest you do something like this:

    my $date_string = &GetValue; my $comparison = &Compare($var1, $date_string); %HoH = ( yadda1 => { key1 => "value1", key2 => $date_string, key3 => $comparison, }, yadda2 => { key1 => "value1", key2 => $date_string key3 => $comparison, }, );
(tye)Re: Re-using a hash value in same hash?
by tye (Sage) on Mar 06, 2001 at 00:20 UTC
    my %HoH = ( yadda1 => { key1 => "value1", key2 => &GetValue, # returns date string }, yadda2 => { key1 => "value1", key2 => &GetValue, # returns date string }, ); for( keys %HoH ) { $HoH{$_}{key3}= &Compare( $var1, $HoH{$_}{key2} ); }

    is one way to do it.

            - tye (but my friends call me "Tye")
Re: Re-using a hash value in same hash?
by boo_radley (Parson) on Mar 06, 2001 at 00:22 UTC
    Since I was starchy in the CB, I'll put in my 2 cents.
    I think the problem is occurring because you're trying to reference HoH during it's creation.

    -deletia-
    update
    well, shoot. :) you got the same piece of advice three four times.
    So there you go.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-16 12:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found