develooper Front page | perl.beginners | Postings from March 2002

I have to Randomize my Query output

Thread Next
From:
FLAHERTY, JIM-CONT
Date:
March 29, 2002 17:36
Subject:
I have to Randomize my Query output
Message ID:
6D0317C617A8D3119EC000A0C9FC45240190AA92@kinx68322m1.cnet.navy.Mil
I am lost . I have this quiz program , It prints and grades the quiz . now
the managers want to make it put out random tests

My Code :



    $dbh =DBI ->connect($data_source, $username, $password) or die "cant
connect
 to $data_source : my $dbh-> errstr\n";
   my $sth1 = $dbh -> prepare("select num from tests where subject = '$test'
");

$sth1 -> execute or die " unable to execute query ";
#$sth1 -> finish;


my $array_ref = $sth1 -> fetchall_arrayref();
   $count = 0;
   foreach $row(@$array_ref){

       my($num) = @$row;
         @numbers[$count] = $num;
         $count++;
     }

  $r_num = rand(@numbers);








$dbh =DBI ->connect($data_source, $username, $password) or die "cant connect
to
$data_source : my $dbh-> errstr\n";
my $sth1 = $dbh -> prepare("select * from tests where num = '$r_num' ");
$sth1 -> execute or die " unable to execute query ";
#$sth1 -> finish;





__________________________________

I figured to query on the test question numbers ( the are unique) then
randomize the numbers. This table has other test questions as well.  When I
randomize the NUM s returns from the query, I thought I would re-query for
the whole record where num = randomize number.  I dont know if that is the
best way to go .

but I am now stuck , its not randomizing . In fact its not doing anything 

help !

Jim 



Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About