http://www.perlmonks.org?node_id=1003504

myelinviolin has asked for the wisdom of the Perl Monks concerning the following question:

I'm writing a code that calculates a bunch of parameters. I say: print "What would you like to know?\n\n"; and the response goes here: $type=<>; Let's say that $INT=5. When the user types in INT, I would like the program to say: INT: 5, or $type: $$type (effectively). And then to keep prompting for user information until they type "exit". I'm not sure how to call out variable names from the user input to display values that have already been calculated previously in the program. I'm sure this is a simple question, or something I shouldn't have to be doing at all. Any help would be appreciated!
  • Comment on Using user input to return variable typed.

Replies are listed 'Best First'.
Re: Using user input to return variable typed.
by BillKSmith (Monsignor) on Nov 12, 2012 at 20:52 UTC
    Store your results in a hash. Use the names as hash keys and the values as hash values. Prompt the use to enter one of the hash keys. Look up the value un the hash. If the key is not in the hash, ask the user to try again.
    Bill
Re: Using user input to return variable typed.
by 2teez (Vicar) on Nov 12, 2012 at 21:23 UTC

    Hi, myelinviolin,
    Please, format your question properly.
    Check this How do I post a question effectively?

    I'm not sure how to call out variable names from the user input to display values that have already been calculated previously in the program

    You can try this,or modify for your usage:

    use warnings; use strict; my %variable_table = ( INT => 5, STR => 'HOME',); while (1) { print "What would you want to know: "; chomp( my $type = <STDIN> ); last if $type eq 'exit'; if ( exists $variable_table{$type} ) { print $type, ' : ', $variable_table{$type}, $/; } else { next; } }

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Using user input to return variable typed.
by marquezc329 (Scribe) on Nov 13, 2012 at 02:02 UTC

    myelinviolin,

    Along with following the advice you've already been given and working through 2teez's example, I'd recommend taking a look at Perlintro (Variable Types) for information on initializing a hash/accessing its key/value pairs, etc. Getting Started with Perl also covers the tools required to solve this problem.

    Don't forget: Copy => Paste doesn't count as learning ;)

Re: Using user input to return variable typed.
by ColonelPanic (Friar) on Nov 13, 2012 at 10:54 UTC
    I agree with others' design suggestions. However, it is worth noting that it is possible to do exactly what you requested:
    use warnings; use strict; my ($thing,$foo,$blah) = (1,2,3); my $done=0; while (!$done) { print "What would you like to know? "; $_=<>; chomp; if (/^exit$/) { $done=1; } else { my $result = eval "\$$_"; if ($@) { print "bad variable name\n"; } else { print "\$$_=$result\n";; } } }
    Generally speaking, eval is not "good design". Why not? Because the user could type this:
    What would you like to know? foo;system("any malicious command here")

    You could make this somewhat safer by sanitizing your input, but it is still better to avoid eval in production code. Besides the security issue, it also makes for confusing, hard to maintain code.

    Nevertheless, I wanted to point this out because it can be very useful, especially for the quick-and-dirty tasks that Perl handles so well.



    When's the last time you used duct tape on a duct? --Larry Wall
      I think I mostly figured out the hash thing. (The answer is always hashes, isn't it!) Thanks guys! BUT for instance I have a variable called CMB, for example, that uses a dice rolling subroutine, that gives me random numbers before the hash. (Can you tell what I'm doing? :P) I'm having a problem with this giving me the same dice roll every time. Let's say A = 14 because I rolled a 14. But, after this value goes in the hash, it gets stuck at 14. So if I want to be able to stay in the program and keep typing in numbers, I will always get a 14 for A. Before it goes in the hash, I can keep getting new numbers for the rolls, but I want to get new numbers afterwards as well. Any help for this?
      sub roll{ my ($a)=@_; #chop($a); ($b,$constant)=split(/\+|\-/,$a); ($number,$dice)=split(/[dD]/, $b); if ($constant==''){ $constant=0; } if($a=~ m/\-/){ $constant*=(-1); } $count=0; for ($c=1; $c<=$number; $c++){ $d[$c]=int(rand($dice))+1; $count += $d[$c]; } $count += $constant; if ($count<0){ $count=0; } return "$count"; } $A=roll("1d20"); $B=roll("1d20"); $C=roll("1d20"); print "BEFORE HASH:$A\t$B\t$C\n"; %hash= ( A => "$A", B => "$B", C => "$C", ); print "What would you like to know?\n\n"; $type=<STDIN>; chomp($type); until ($type eq "Q"){ #print "You typed .$type.\n"; print "\n\n$type: $hash{$type}\n"; print "What would you like to know?\n\n"; $type=<STDIN>; chomp($type); }

        What is in $b? Where is $CMB? Why do you expect the value stored in a hash to change once it's been set? Why are you not rolling the die each time the user inputs a request?

        If the computer were a person, here is what that person is doing currently:

        1. Roll a die, and write down the result.
        2. Roll another, and write down the result.
        3. Roll again and write down the result.
        4. Read the first result and write it again into a little book of indexes and corresponding values.
        5. Read the second result and write it again into a little book of indexes and corresponding values.
        6. Read the third result and write it again into a little book of indexes and corresponding values.
        7. Repeatedly ask someone what they would like to know.
        8. Look in your little index book and find the answer that was written down for the question that was asked.
        9. Read back to the user the answer that you wrote in your little book.
        10. Wonder why the answer written in the little book hasn't been magically erased and re-written without your intervention.

        In real life, as a person, you would roll the dice each time someone requests a roll. In your program, you roll just one time and store the result. I don't think that caching a die roll is the right approach.


        Dave

        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.

Re: Using user input to return variable typed.
by davido (Cardinal) on Nov 13, 2012 at 18:17 UTC

    A dice roll script is generally useful. Minimally (without getting golfy), it could look like this:

    use strict; use warnings; while(1) { print "Enter a dice to roll ('q' to quit): "; my $input = <STDIN>; last if $input =~ m/^q/; my( $rolls, $die ) = $input =~ m/(\d+)d(\d+)/i; my $result = 0; $result += 1 + int rand $die for 1 .. $rolls; print $result, "\n"; }

    If I plan to use it as more than a throwaway script, here's how I would do it:

    use 5.010; use strict; use warnings; use Readonly; use Games::Dice::Advanced; use IO::Prompt::Hooked; Readonly my $RE_GOOD_ROLL => qr{ ^\s* # Start of line, ignore +whitespace (?&POS_INT)? # How many rolls (option +al). \s* d \s* # 'd' (forgiving of whit +espace) (?&POS_INT) # Die to roll. \s*$ # End of input, ignore w +hitespace. (?(DEFINE) (?<POS_INT> [1-9][0-9]{0,2}? ) ) # Definition of positive + int. }x; Readonly my $DEFAULT_ROLL => '1d6'; my $roll; while (defined(my $input = ask($roll // $DEFAULT_ROLL))) { $roll = interpret_input($input); my $result = Games::Dice::Advanced->roll($roll); print "Result of ( $roll ) is [ $result ].\n\n"; } sub interpret_input { my $i = shift; $i =~ s/\s+//g; $i =~ s/^d/1d/; return $i; } sub ask { my $default = shift; return prompt( default => $default, message => 'Enter a dice roll ("q" to quit): ', validate => $RE_GOOD_ROLL, error => "Input must be in format of d8, or 2d6.\n\n", escape => qr/^ \s* q (?: uit )? /ix, # q or quit, case insens +itively. ); }

    The second snippet adds input validation, default input, and allows input like "d6" as synonymous with "1d6".

    This example sets a default roll of '1d6'. It then prompts the user to enter a roll, 'q' or 'quit' to quit (case insensitively), or 'enter' to accept the default roll. After the first roll, all future rolls will have their default set to the previous roll, so that a user can enter '2d20', and then just hit enter again to repeat the roll.

    User input is validated, and constrained to three digit roll counts, and three digit dice. Upon receiving invalid input, a message is provided to the user explaining valid input.

    While it's not difficult to build a dice roll generator, I used Games::Dice::Advanced because it is already more fully developed, has its own reasonable error handling, and is a good fit for our use. IO::Prompt::Hooked handles the prompting, and facilitates validation, defaults, bad input message, and input termination. We also define a regex snippet that matches basic non-zero positive integers of up to three digits, for our roll count and die type.

    Here's a sample run:

    $ ./diceroll.pl Enter a dice roll ("q" to quit): [1d6] Result of ( 1d6 ) is [ 3 ]. Enter a dice roll ("q" to quit): [1d6] 2d4 Result of ( 2d4 ) is [ 3 ]. Enter a dice roll ("q" to quit): [2d4] 3d100 Result of ( 3d100 ) is [ 122 ]. Enter a dice roll ("q" to quit): [3d100] Result of ( 3d100 ) is [ 153 ]. Enter a dice roll ("q" to quit): [3d100] 34c Input must be in format of d8, or 2d6. Enter a dice roll ("q" to quit): [3d100] d6 Result of ( 1d6 ) is [ 2 ]. Enter a dice roll ("q" to quit): [1d6] q

    Dave