Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: Dynamic Hash value

by almut (Canon)
on Apr 21, 2010 at 08:01 UTC ( #835986=note: print w/replies, xml ) Need Help??


in reply to Dynamic Hash value

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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2023-09-30 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?