Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

How to refer to a scalar variable dynamically?

by noslenj123 (Scribe)
on Apr 16, 2003 at 20:26 UTC ( [id://251030]=perlquestion: print w/replies, xml ) Need Help??

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

I have code like this:
my $login_tot; my $campaign_tot; my $work_tot; foreach my $func ('login','campaign','work') { # do some things my $tmp = calc_difference($var1, $var2); # update _tot vars here }
What I need in the "foreach" loop is a way to update the "_tot" variables. I have tried things like:

${$func . "_tot"} += $tmp;
or
my $ref = "$func" . "_tot"; $$ref++;
Not working, so I don't understand how the indirection or referencing works. Any ideas on how I can get a clue??

Replies are listed 'Best First'.
Re: How to refer to a scalar variable dynamically?
by Mr. Muskrat (Canon) on Apr 16, 2003 at 20:33 UTC

    Use a hash.

    my %total = ( login => 0, campaign => 0, work => 0, ); foreach my $func (keys %total) { # qw(login campaign work) # do some things my $diff = calc_difference($var1, $var2); $total{$func} += $diff; }

Re: How to refer to a scalar variable dynamically?
by adrianh (Chancellor) on Apr 16, 2003 at 20:37 UTC
      Those are great articles and now I understand why not to do what I was trying to do. Of course it makes me feel stupid and humble, but I just don't see any other way to learn, do you? :-)

      Me thinks I'll just use a hash...

        Nobody knows everything. The only way to find out is to ask questions ;-)

        Don't feel too stupid, I had the same question, and the answer helped me too. :-)
Re: How to refer to a scalar variable dynamically?
by dragonchild (Archbishop) on Apr 16, 2003 at 20:33 UTC
    Use a hash.
    my %totals = ( login => 0, campaign => 0, work => 0, ); foreach my $func (qw(login campaign work)) { # Do stuff my $tmp = calc_difference($var1, $var2); $totals{$func} += $tmp; }
    Anytime you think you want to use soft references, look real hard at hashes instead.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: How to refer to a scalar variable dynamically?
by perlplexer (Hermit) on Apr 16, 2003 at 20:34 UTC
    You could accomplish that by using "soft" references but it is not recommended to use them since they pollute your namespaces.
    Instead, use a hash; e.g.,
    my %vars = ( 'login' => undef, 'campaign' => undef, 'work' => undef, ); foreach my $func ('login','campaign','work') { my $tmp = calc_difference($var1, $var2); $vars{$func}{tot} = $tmp; # Or just append 'tot' to the key, e.g., # $vars{"${func}_tot"} = $tmp; }
    --perlplexer
Re: How to refer to a scalar variable dynamically?
by Elian (Parson) on Apr 16, 2003 at 20:41 UTC
    It doesn't work because you can't refer to my variables symbolically. That only works with globals. Just no joy there. (And no, for the record it won't be changed for perl 6, not that you've asked :)

    Since what you want to do won't work, it's time to look for alternate solutions.

      But we will have %MY won't we?

      -Lee

      "To be civilized is to deny one's nature."
        Sure, but that still won't make simple symbolic dereferencing work with lexicals...
Re: How to refer to a scalar variable dynamically?
by artist (Parson) on Apr 16, 2003 at 20:35 UTC
    You may want to use hash in this case.
    Store functions as 'keys' and totals as 'values'
    my %total; foreach my $function (qw(login campaign work)){ # do some things my $tmp = calc_difference($var1, $var2); $total{$function} += $tmp; } foreach my $function ( keys %total){ print "$function =>",$total{$function},"\n"; } #Single function print $total{login};
    artist
Re: How to refer to a scalar variable dynamically?
by dmitri (Priest) on Apr 16, 2003 at 22:11 UTC
    Isn't it a little ridiculous that 5 people posted the same solution within 4 seconds time? :)

      It will only be ridiculous if the poster chooses to ignore the good advice. If we see a follow up, anyone care to place odds as to the response? :)

      Cheers,
      Ovid

      New address of my CGI Course.
      Silence is Evil (feel free to copy and distribute widely - note copyright text)

        A humble little poster like me would not even contemplate ignoring the wisdom of the masters. The proxy at work went down after I posted and I could not get back til now.

        Having the same answer by several monks certainly leaves no doubt as to the validity of that answer. Having them so close together makes one think that there is a competition going on. ;-)

        Thank you all so much. I have no problem using a hash, in fact I've used them like this before. Don't know why I was hell bent on using a scalar.

        What will the odds be Ovid? :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2024-04-18 12:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found