sub generate{ my $self=shift; my $Total_Questions=_get_FileLength($self); my $FileName=_get_FileName($self); my $Data=shift; my $Max_Questions=shift; $Max_Questions = $Total_Questions unless defined $Max_Questions; croak"Number of questions in $FileName exceeded" if $Max_Questions > $Total_Questions; croak"Must have at least one question in test" if $Max_Questions < 1; &_set_Max_Questions($self,$Max_Questions); # # Original Code # # if(!defined $Max_Questions){ # $Max_Questions=$Total_Questions; # &_set_Max_Questions($self,$Max_Questions); # }elsif($Max_Questions > $Total_Questions){ # croak"Number of questions exceeds the amount in $FileName"; # }elsif($Max_Questions < 1){ # croak"Must have at least one question in the test"; # }else{ # &_set_Max_Questions($self,$Max_Questions); # } my %Randoms=(); my @Randoms=(); my %Test_Questions=(); my %Test_Answers=(); my %Question_Lengths=(); my $E; for(1..$Max_Questions){ my $question_number=int(rand($Total_Questions)+1); redo if exists $Randoms{$question_number}; $Randoms{$question_number}=1; } @Randoms=keys %Randoms; &_shuffle(\@Randoms); for(my $D=0;$D<$Max_Questions;$D++){ $Test_Answers{$Randoms[$D]}=pop @{$$Data{$Randoms[$D]}}; $Test_Questions{$Randoms[$D]} = $$Data{$Randoms[$D]}; } foreach my $key(keys %Test_Questions){ $E=@{$Test_Questions{$key}}; $Question_Lengths{$key}=$E; } return \%Test_Questions,\%Test_Answers,\%Question_Lengths,\@Randoms; }
In reply to Re: Optimization of a piece of code(Is there a better way than this?)
by TStanley
in thread Optimization of a piece of code(Is there a better way than this?)
by TStanley
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |