Jim Gibson wrote:
On 3/24/11 Thu Mar 24, 2011 9:04 AM, "Chris Stinemetz"
<cstinem...@cricketcommunications.com> scribbled:
$sum{$cell}{$sect}{$carr} += $rlp1 += $rlp2 += $rlp3 += $rlp4 || 0 ; }
I see you are changing your program requirements. You are now accumulating
multiple rlp values instead of one.
This line has two problems. The first is that you have placed the closing
brace at the end of the line where it is easy to lose. Perhaps this is why
you have mistakenly used the $dist variable in the lines below, where it is
not in scope. The second problem is the multiple '+=' operators. Do you know
what they will do in this case? I don't. I would have to run a test to see
what the effect is on the intermediate variables. You might be doing more
arithmetic than is necessary.
Yes it is. That is modifying $rlp1, $rlp2 and $rlp3 as well as
$sum{$cell}{$sect}{$carr}.
And because the = operator is right associative $rlp3 is modified first
and then $rlp2 and then $rlp1 and finally $sum{$cell}{$sect}{$carr}.
Observe:
$ perl -le'
package JWK;
use Carp;
sub TIESCALAR {
my $class = shift;
my $value = shift || 0;
carp "JWK::TIESCALAR got $value";
return bless \$value, $class;
}
sub FETCH {
my $self = shift;
confess "wrong type" unless ref $self;
croak "usage error" if @_;
carp "JWK::FETCH returned $$self";
return $$self;
}
sub STORE {
my $self = shift;
confess "wrong type" unless ref $self;
$$self = shift;
carp "JWK::STORE set to $$self";
croak "usage error" if @_;
}
sub DESTROY {
my $self = shift;
confess "wrong type" unless ref $self;
carp "JWK::DESTROY $$self";
}
package main;
tie my $x, "JWK", 23;
tie my $y, "JWK", 34;
tie my $z, "JWK", 45;
my $count = $x + $y + $z;
print "\$count = $count";
my $count = $x += $y += $z;
print "\$count = $count";
'
JWK::TIESCALAR got 23 at -e line 31
JWK::TIESCALAR got 34 at -e line 32
JWK::TIESCALAR got 45 at -e line 33
JWK::FETCH returned 34 at -e line 35
JWK::FETCH returned 23 at -e line 35
JWK::FETCH returned 45 at -e line 35
$count = 102
JWK::FETCH returned 45 at -e line 37
JWK::FETCH returned 34 at -e line 37
JWK::STORE set to 79 at -e line 37
JWK::FETCH returned 79 at -e line 37
JWK::FETCH returned 23 at -e line 37
JWK::STORE set to 102 at -e line 37
JWK::FETCH returned 102 at -e line 37
$count = 102
JWK::DESTROY 45 at -e line 0
JWK::DESTROY 79 at -e line 0
JWK::DESTROY 102 at -e line 0
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/