Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

how to grab random questions from file?

derasderas Posts: 3Member
i have a test script which grabs 5 of 5 questions from nine different categories (Question [1-9]) as shown...

-----------------------------------------------------------------
open FDESK, "questions.txt" or die print "
Cannot open file questions.txt: $!";
flock FDESK, LOCK_SH;
@quest=;
close FDESK;
chomp @quest;
$num=1;
for ($i=0; $i<=$#quest; $i++)
{
next unless $quest[$i]=~/Question [1-9]/;
for ($j=1; $j<=5; $j++)
--------------------------------------------------------------

i want to increase the number of questions to @15 in questions.txt and then have the script randomly select 5 questions out of those 15 (not the first five everytime). how would i implement such a randomized question grabbing in the above code?

Comments

  • JonathanJonathan Posts: 2,914Member
    Hi,

    Different context, same solution. Time for my random array element selection one-liner again. :-)

    Put all your questions in @questions - one question in each array element (that's what you're doing now, I think). Then do this:-

    @chosen = grep {$::z++ < 5} sort { int(rand() * 2) } @questions, $::z = 0;

    @chosen will contain 5 questions out of @questions, randomly selected. If you're not sure how it works, here's the explanation I posted last time I pulled this one out:-

    http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=12&MsgID=218626&Setting=A9999F0003

    Hope this helps,

    Jonathan

    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

  • derasderas Posts: 3Member
    the thing is there are nine categories of questions, Question 1, Question 2... so i want 5 random questions from each of the nine categories.

    : Hi,
    :
    : Different context, same solution. Time for my random array element selection one-liner again. :-)
    :
    : Put all your questions in @questions - one question in each array element (that's what you're doing now, I think). Then do this:-
    :
    : @chosen = grep {$::z++ < 5} sort { int(rand() * 2) } @questions, $::z = 0;
    :
    : @chosen will contain 5 questions out of @questions, randomly selected. If you're not sure how it works, here's the explanation I posted last time I pulled this one out:-
    :
    : http://www.programmersheaven.com/c/MsgBoard/read.asp?Board=12&MsgID=218626&Setting=A9999F0003
    :
    : Hope this helps,
    :
    : Jonathan
    :
    : ###
    : for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    : (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    : /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");
    :
    :

  • JonathanJonathan Posts: 2,914Member
    : the thing is there are nine categories of questions, Question 1,
    : Question 2... so i want 5 random questions from each of the nine
    : categories.
    OK, so you have an array of arrays (actually, an array of array references). Then you loop over the main array with the 9 groups of questions in, and use the code I gave you to select 5 questions from each group.

    Jonathan


    ###
    for(74,117,115,116){$::a.=chr};(($_.='qwertyui')&&
    (tr/yuiqwert/her anot/))for($::b);for($::c){$_.=$^X;
    /(p.{2}l)/;$_=$1}$::b=~/(..)$/;print("$::a$::b $::c hack$1.");

Sign In or Register to comment.