Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Using user input to return variable typed.

by thmsdrew (Scribe)
on Nov 13, 2012 at 16:01 UTC ( #1003645=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Using user input to return variable typed.
in thread Using user input to return variable typed.

A new number will be generated when roll is called. If you want to get a new number, then the value, of "A" for example, needs to be updated each time you want to get it. Once it's stored in a hash, it's stored in a hash. Referring to the variable in the code won't update the value, even if you put the call to roll("1d20") directly into the hash, as in:

%hash = ( A => roll("1d20"), B => roll("1d20"), C => roll("1d20"), ); # As soon as you get to this point in the code, the value is stored + (for good).

You need to figure out when you will want to store the value that was rolled, and when you will want to get a new value, and handle that accordingly. If the user wants to get a new "A" value, then you need to call roll again. If they want to get an old "A" value, then you need to refer to the value stored in the hash.

It seems like the point of the hash in your situation is to store things that don't dynamically change, but things that change as a result of the user doing something. However, if you want a roll to occur, you'll want a different mechanism for actually getting the roll, and the hash will be used just to store that roll.

I hope I am making sense.


Comment on Re^3: Using user input to return variable typed.
Select or Download Code
Re^4: Using user input to return variable typed.
by myelinviolin (Novice) on Nov 13, 2012 at 16:21 UTC
    It had seemed weird to do ALL my calculations each time a person asked something, but that's all I had to do, was to move all the calculations inside the loop where it asks for input. Since I had just figured out how hashes worked, so was still confused on how they worked. I know it all seems so simple once you have the answer, so thanks for putting up with me!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2014-09-01 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (8 votes), past polls