Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
my $rand_position = $positions_ref->[ rand( $#{$positions_ref} ) ]; my $rand_location = $locations_ref->[ rand( $#{$locations_ref} ) ];

You have an off-by-one error on those array subscripts.   'in the kitchen' will never be chosen from @locations and 'reclining' will never be chosen from @positions.

I would write it more like this:

#!/usr/bin/perl use strict; use warnings; my @positions = qw( sitting standing reclining ); my @locations = ( 'at the museum', 'in the woods', 'on the beach', 'in + the kitchen' ); my @drinks = qw( scotch beer coke yoohoo ); 1 while do_it( print_options( \@drinks ), \@positions, \@locations ); sub print_options { my $drinks_ref = shift; for my $i ( 0 .. $#$drinks_ref ) { print $i + 1, ". $drinks_ref->[$i]\n"; } my $selection; do { print 'Choose a number: '; chomp( $selection = <STDIN> ); } until $selection >= 1 && $selection <= @$drinks_ref; return $drinks_ref->[ $selection - 1 ]; } sub do_it { my ( $drink, $positions_ref, $locations_ref ) = @_; my $rand_position = $positions_ref->[ rand @$positions_ref ]; my $rand_location = $locations_ref->[ rand @$locations_ref ]; print "You just drank a $drink $rand_location in the $rand_positio +n position.\n"; if ( $rand_location eq 'on the beach' ) { print "YEEEEEHAAAWWWWWWWWWW!\n"; return 0; } return 1; }

In reply to Re: Repeating an input prompt by jwkrahn
in thread Repeating an input prompt by zod

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 14:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found