Re: How to refer to a scalar variable dynamically?
by Mr. Muskrat (Canon) on Apr 16, 2003 at 20:33 UTC
|
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;
}
| [reply] [d/l] |
Re: How to refer to a scalar variable dynamically?
by adrianh (Chancellor) on Apr 16, 2003 at 20:37 UTC
|
my %totals;
foreach my $func ('login','campaign','work') {
my $tmp = calc_difference($var1, $var2);
$total{$func) += $tmp;
}
If you really need to do it do a super search on symbolic references :-) | [reply] [d/l] |
|
| [reply] |
|
| [reply] |
|
Don't feel too stupid, I had the same question, and the answer helped me too. :-)
| [reply] |
Re: How to refer to a scalar variable dynamically?
by dragonchild (Archbishop) on Apr 16, 2003 at 20:33 UTC
|
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. | [reply] [d/l] |
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 | [reply] [d/l] |
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. | [reply] [d/l] |
|
But we will have %MY won't we?
-Lee
"To be civilized is to deny one's nature."
| [reply] |
|
Sure, but that still won't make simple symbolic dereferencing work with lexicals...
| [reply] |
|
|
|
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 | [reply] [d/l] |
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? :)
| [reply] |
|
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)
| [reply] |
|
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? :-)
| [reply] |
|
|
| [reply] |