in reply to Question regarding variable scope

The answer is yes, and I have modified your code slightly, the code below should work -

sub find_paring() { my $white_player = shift; my $black_player = "none"; # Get a list of players still in the running my @possible_opponents = keys %{$matches_remaining{$white_player}}; # Shuffle the list to add some randomness, then find the first # avaliable opponent. &shuffle(\@possible_opponents); foreach (@possible_opponents) { # Once we have a valid match, we remove the entries from the # match hash, and return the pair. if (exists $matches_remaining{$_}{$white_player}){ $black_player = $_; delete $matches_remaining{$white_player}{$_}; delete $matches_remaining{$_}{$white_player}; last; } } # Return the info we have return ($white_player, $black_player); }

Replies are listed 'Best First'.
Re: Re: Question regarding variable scope
by Rhandom (Curate) on Oct 17, 2003 at 15:25 UTC
    Hmmmm....
    sub find_paring() { my $white_player = shift;
    Looks like a conflict of interest there. Can't really shift if you aren't allowing anything to be passed. Try
    sub find_paring { my $white_player = shift;
    I think you'll be happier.
    my @a=qw(random brilliant braindead); print $a[rand(@a)];
      Umm, I have to disagree with you here. The code
      sub find_parsing() { ... }
      is equivalent to
      sub find_parsing { ... }
      I always start my subroutines with sub function() (a habit from shell programming days). Strict argument prototyping is not necessary with Perl though.