Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re-assign value to subroutine

by props (Hermit)
on Nov 19, 2007 at 21:47 UTC ( [id://651775]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks The rand function can either get as an input the number 10 or user's choice.How to set the initial rand input to 10 when i start with option 1 because now there is no number 10 as an input for rand, I think if i start with option 2 it will work.Is it possible one subroutine to handle both cases, i tried but i didn't succeeded.
$my number =10; while($input=<STDIN>){ print"choose 1 or 2 \n"; $choice=(<STDIN>); if($choice=~/1/){ &flashcard(); else{ $choice=~/2/); print"enter random range\n"; my $newrandom = (<STDIN>); &options($newrandom); } } sub flashcard { my $random = int(rand(&options())); } sub options { my $max = shift; if ($max==10){ return 10; }else{ return $max; } }
Many thanks

Replies are listed 'Best First'.
Re: Re-assign value to subroutine
by ysth (Canon) on Nov 19, 2007 at 22:38 UTC
    Your description is a little confusing, but I'm guessing you want to have choice 1 do something based on an option and choice 2 set the option (with choice 1 using a default value if the option isn't set yet). If so, you need somewhere to store the option. Your sub options could do this itself:
    { my $saved_option = 10; # default value is 10 sub options{ # were we passed a value to save for later? if (@_) { $saved_option = shift; } return $saved_option } }
    or just use a variable instead of the subroutine:
    my $option = 10; while (1) { print"choose 1 or 2 \n"; my $choice=(<STDIN>) or last; if ($choice == 1) { &flashcard($option); } elsif ($choice == 2) { print"enter random range\n"; my $option = (<STDIN>); } else { last } } sub flashcard { my $option = shift; my $random = int(rand($option)); # do stuff with $random }
    By the way, your code had so many errors, it's clearly not what you were trying. It really helps if you show the exact code you are working with.
      Here is the initial code
      use warnings; use strict; print "Welcome to Multiplication Challenge\n"; print "What would you like to do?\n"; print "1) FLASHCARD MODE\n"; print "2) TEST MODE\n"; print "3) OPTIONS\n"; print "4) Exit\n"; my $input = undef; my $max = undef; my $min = 10; sub menu { print "What would you like to do?\n"; print "1) FLASHCARD MODE\n"; print "2) TEST MODE\n"; print "3) OPTIONS\n"; print "4) Exit\n"; } while ( $input = (<STDIN>) ) { if ( $input =~ /1/ ) { print "FLASHCARD MODE\n"; flashcard(); } elsif ( $input =~ /2/ ) { print "TEST MODE\n"; testmode(); } elsif ( $input =~ /3/ ) { print "3\n"; print "What is the maximum number?\n"; my $max = (<STDIN>); &makeitbig($max); &menu(); } elsif ( $input =~ /4/ ) { really(); } } sub really { print "Are you sure?\n"; my $input = (<STDIN>); if ( $input =~ /y/ ) { exit; } else { menu(); } } sub flashcard { for ( 1 .. 10 ) { my $random1 = int( rand(10) ); my $random2 = int( rand(10) ); print "$random1 * $random2\n"; my $total = ( $random1 * $random2 ); &validate($total); } } my $count = 1; sub validate { my $newdata = shift; TRYAGAIN: my $input = (<STDIN>); if ( $input == $newdata ) { print "Correct!\n"; $count++; if ( $count == 10 ) { print "Congratulations\n"; print "You have finished FLASHCARD MODE;\n"; menu(); } } else { print "$count: Try again\n"; goto TRYAGAIN; } } sub testmode { for ( 1 .. 25 ) { my $random1 = int( rand(10) ); my $random2 = int( rand(10) ); print "Problem '#'$_\n"; print "$random1 * $random2\n"; my $total = ( $random1 * $random2 ); &validate_low($total); } } my $meter = 1; my @correct = (); my $correctanswer = 1; sub validate_low { my $newdata = shift; my $input = (<STDIN>); $meter++; if ( $input == $newdata ) { print "Correct!\n"; push( @correct, $correctanswer ); } else { print "Sorry!\n"; } if ( $meter == 25 ) { &calculate(); } } my $sum = 0; sub calculate { foreach (@correct) { $sum += $_; return $sum; } my $prcnt = 4 * $sum; print "You got $sum answers correctly out of 25. This is $prcnt%\n +"; } sub makeitbig { my $other = shift; if ( $other == $min ) { return $min; } else { return $other; } }
Re: Re-assign value to subroutine
by swampyankee (Parson) on Nov 19, 2007 at 23:09 UTC

    rand returns a random1, uniformly distributed number. To quote the documents, "Returns a random fractional number greater than or equal to 0 and less than the value of EXPR. (EXPR should be positive.) If EXPR is omitted, the value 1 is used. Currently EXPR with the value 0 is also special-cased as 1 - this has not been documented before perl 5.8.0 and is subject to change in future versions of perl."

    So, if you pass 0 to rand(), it currently returns a value, x|0 ≤ x < 1. I believe it does the same think if you pass undef to rand, but RTFM2. How you handle your value of number (incidentally,

    $my number = 10;
    is not valid; I'm guessing you meant to write:
    my $number = 10;
    but I'm sure you noticed the error message from perl!) depends on what you want to do. Possibly, something like
    $value = $user_input or 10; $x = rand($value);
    may suffice, but I think that it most likely won't.

    Now, if you want to guarantee that rand always returns the same sequence of values, you've got to explicitly seed it, using srand. Once.


    1Pedantically, they're likely not truly random; they're almost certainly pseudo-random.

    2The manual is definitely a worthwhile read, too. As an old connoisseur of computing documents, Perl's is definitely deserving of accolades.


    emc

    Information about American English usage here and here.

    Any Northeastern US area jobs? I'm currently unemployed.

    </div
Re: Re-assign value to subroutine
by ikegami (Patriarch) on Nov 19, 2007 at 22:36 UTC

    I don't know what you are trying to do. "set the initial rand input" makes no sense. Could you clarify? In the meaning time, check out these problems with your code.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-03-28 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found