Thanks to all for your suggestions, and I now give you the final code release (the entire subroutine where the snippet is found):
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; }


TStanley
--------
Never underestimate the power of very stupid people in large groups -- Anonymous

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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.