#! perl use strict; use warnings; use Data::Dumper; use constant { WELL => 0, SICK => 1, }; my @pop = ( { name => 'Fred', status => WELL, }, { name => 'Wilma', status => WELL, }, { name => 'Barney', status => WELL, }, { name => 'Betty', status => WELL, }, ); (my $pop = scalar @pop) > 0 or die "Empty population\n"; print "How many random infections? (Enter a number between 1 and $pop):\n"; chomp(my $answerinfect = <>); ($answerinfect > 0) && ($answerinfect <= $pop) or die "Invalid selection\n"; for (1 .. $answerinfect) { for (my $new_infection = 0; !$new_infection;) { my $contact = int(rand($pop)); if ($pop[$contact]{status} == WELL) { $pop[$contact]{status} = SICK; $new_infection = 1; } } } print Dumper(\@pop); printf "You have infected %d %s: ", $answerinfect, ($answerinfect == 1) ? 'person' : 'people'; print join(', ', sort map { $_->{name} } grep { $_->{status} == SICK } @pop);