Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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.

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003645]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2017-01-23 01:12 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (190 votes). Check out past polls.