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

Forgive me, Oh Wise Ones, if you've heard this one before, but my many prayers to the great god FAQ have not brought enlightenment. I'm attempting to use a variable within a hash value, like so:
$magic=2; %items=(1, ($magic*2)); print %items; $magic+=1; print %items;
In this example, the value of $items{1} is 4 before and after the value of $magic changes. I'm obviously missing something, but I'm confused. I'm also VERY new at this, so please be gentle :) -Pooh

Replies are listed 'Best First'.
Re: problem with variables in hash values??
by dws (Chancellor) on Dec 05, 2001 at 04:15 UTC
    In this example, the value of $items{1} is 4 before and after the value of $magic changes.

    As it should be. When you initialize %items, you are using the result of calculating $magic*2. This calculation is done once, not automagically each time $magic subsequently changes.

      Bah! So using a variable there is not useful. Hopefully I'll discover another way to do this when I learn more. Well, at least now I know, so I won't spend hours trying to figure out how to make it work.

      Thanks!!

        Consider a *reference* to a variable:
        $magic=4; #same as 2*2 last time I checked ;-) %items=(1 => \$magic); print "1: ${$items{1}}\n"; $magic+=1; print "1: ${$items{1}}\n";
        See perlref

        --
        perl -p -e "s/(?:\w);([st])/'\$1/mg"

Re: problem with variables in hash values??
by gt8073a (Hermit) on Dec 05, 2001 at 14:31 UTC

    In response to belg4mit's advice: Consider a *reference* to a variable,
    I recommend references and anonymous subroutines like the updated following:

    #!/usr/bin/perl -w use strict; ## variables my $magic = 2; my $sub = sub { $_[0] * $_[1] }; # sub ref my %item; ## subroutine sub tpl { $magic * 3 }; ## populate our hash $item{ '1' } = \$magic; # ref $item{ 'dbl' } = sub { $magic * 2 }; # anonymous sub $item{ 'tpl' } = sub { &tpl() }; # anon sub calls sub $item{ 'x' } = sub { $sub->( $magic, $_[0] ) }; # anon sub calls sub ref ## fool with $magic value, ## display results print "Magic: $magic\t1: ", ${$item{1}}, "\tdbl: ", &{$item{ 'dbl' }}(), "\ttpl: ", $item{ 'tpl' }->(), "\tx(4): ", $item{ 'x' }->(4), "\n"; $magic += 1; print "Magic: $magic\t1: ", ${$item{1}}, "\tdbl: ", $item{ 'dbl' }->(), "\ttpl: ", &{$item{ 'tpl' }}(), "\tx(4): ", $item{ 'x' }->(4), "\n"; $magic -= 1; print "Magic: $magic\t1: ", ${$item{1}}, "\tdbl: ", &{$item{ 'dbl' }}(), "\ttpl: ", $item{ 'tpl' }->(), "\tx(4): ", &{$item{ 'x' }}(4), "\n"; $magic *= 5; print "Magic: $magic\t1: ", ${$item{1}}, "\tdbl: ", $item{ 'dbl' }->(), "\ttpl: ", &{$item{ 'tpl' }}(), "\tx(4): ", $item{ 'x' }->(4), "\n"; ## we're good print "Done\n\n"; exit(0);

    there is quite a bit in there to play with, but it should show you how to use references and anonymous subroutines. notice the different syntax you can use, &{$item{'x'}}(4) and $item{'x'}->(4). be aware, though, that there is no parameter validation, and the output is sloppy

    I'm also VERY new at this, so please be gentle
    sub referrences are generally daunting, and not so gentle, for the new. but, once you get over your initial fear, they really aren't too difficult to understand.

    for more information, look here for tye's tutorial on reference
    here and here for a little info on "closures", which build on subroutine references
    and, as mentioned above, perlref

    hang in there, and piece through the code and few links i gave you. while they may not answer your question out right, they are a start.

    Will perl for money
    JJ Knitis
    (901) 756-7693
    gt8073a@industrialmusic.com