Perl

Moderators: Jonathan
Number of threads: 1259
Number of posts: 3644

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
how to grab random questions from file? Posted by deras on 25 Nov 2003 at 6:52 AM
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 "<br>Cannot open file questions.txt: $!";
flock FDESK, LOCK_SH;
@quest=<FDESK>;
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?
Report
Re: how to grab random questions from file? Posted by Jonathan on 25 Nov 2003 at 8:18 AM
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.");

Report
Re: how to grab random questions from file? Posted by deras on 25 Nov 2003 at 3:38 PM
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.");
:
:

Report
Re: how to grab random questions from file? Posted by Jonathan on 27 Nov 2003 at 3:10 AM
: 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.");




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.