Another version, using a tied hash:
#!/usr/bin/perl
package DynHash;
use Tie::Hash;
our @ISA = 'Tie::StdHash';
sub TIEHASH {
my $class = shift;
return bless {}, $class;
}
sub FETCH {
my $self = shift;
return ${ $self->{$_[0]}[0] } - ${ $self->{$_[0]}[1] }
}
package main;
use Data::Dumper;
tie my %result, "DynHash";
my $ini = 4;
my $last = 2;
%result = (
SAS1 => [ \$ini, \$last ],
SAS2 => [ \$last, \$ini ],
SAS3 => [ \$ini, \$last ]
);
print Dumper \%result;
$ini = 10;
$last = 5;
print Dumper \%result;
__END__
$VAR1 = {
'SAS3' => 2,
'SAS2' => -2,
'SAS1' => 2
};
$VAR1 = {
'SAS3' => 5,
'SAS2' => -5,
'SAS1' => 5
};
i.e., store references to $ini and $last in the hash, and dynamically recompute the difference from them at the time the values of the (tied) hash are being fetched.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|