http://qs321.pair.com?node_id=835995

in reply to Re: Dynamic Hash value

Actually Like SAS1, SAS2, SAS3, ...etc are there. Based on those value i am calculating the value say \$some_value.

which(\$some_value) may either \$ini-\$last or \$last-\$ini

So for that i am building hash %result with SAS1, SAS2., SAS.. as key and either \$ini-\$last or \$last-\$ini as value

So later in the code if i change the values of either \$ini or \$last then this changed value should reflect in the hash such that whenever i retrieved hash value using SASx as key then it should give the latest result

.

If You wants to suggest something like Tie:Scalar please bit elaborate on Tie:Scalar thing as i dont know about that

Because somebody has suggested that. But i didn't get that. I found Corion's example is good actually

Thanks

Replies are listed 'Best First'.
Re^3: Dynamic Hash value
by mzedeler (Pilgrim) on Apr 21, 2010 at 20:22 UTC

It is a most unusual way to solve this kind of selection problems by using references. You get into the dreaded Action at A Distance anti-pattern by doing so. It is much more obvious to just write a function:

```my \$last = 0;
my \$ini = 0;

...

\$last = <something else>;

...

\$ini = <something else>;

... # now you need \$some_value:

my \$selector = 'SAS1';
my \$some_value = compute_sas(\$selector, \$ini, \$last);

sub compute_sas {
my(\$selector, \$ini, \$last) = @_;

if(\$selector eq 'SAS1') {
return \$ini - \$last;
} else {
return \$last - \$ini;
}
}

Note that the sub compute_sas is sloppy, untested and probably not giving the right results, but it should illustrate my point.

Re^3: Dynamic Hash value
by LanX (Saint) on Apr 21, 2010 at 08:59 UTC
> If You wants to suggest something like Tie:Scalar please bit elaborate on Tie:Scalar thing as i dont know about that

Because somebody has suggested that. But i didn't get that.

I suggested others already "elaborated", but it seems you didn't read ...

Paṛha kr̥payā!

> I found Corion's example is good actually
>