Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Dynamic Hash value

by GrandFather (Saint)
on Apr 21, 2010 at 08:04 UTC ( #835987=note: print w/replies, xml ) Need Help??


in reply to Dynamic Hash value

Why?

Although there are ways to achieve the effect you describe (as others have indicated), it is likely that what you want to do can best be approached in a different way. Corion's sub get_SAS is a good example of how a very slight rethink of the problem may yeld a better solution, but we can't help you best (except by guessing and getting lucky) unless you supply the bigger picture.

Given that you show the result of printing the values contained in the hash it may be that you don't need a hash at all, but that an array may be more appropriate. How can we tell?

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Dynamic Hash value
by Bheema_Tyco (Novice) on Apr 21, 2010 at 08:45 UTC

    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

    Please help me

    Thanks

      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.

      > 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
      >
      > Please help me

      My helpful advice: Use Corions example!

      > Thanks

      Your welcome! 8)

      Cheers Rolf

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (2)
As of 2023-10-04 14:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?