I have numbers in an array how do extract them in a random order ?

2002-04-10 Thread FLAHERTY, JIM-CONT

I have numbers in an array how do extract them in a random order ? . I want
to feed them to a sql query to extract information , just the first 10 of
them 
 
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;
 

   $count1 = 0;
  my $array_ref = $sth1->fetchall_arrayref();
 
  foreach $row(@$array_ref){
 
 $num1 = $row->[0];
 
 push(@numbers, $num1);
$count1++;
  }
 

###
#   now to extract them 
 
$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 = '$num1' ");
$sth1 -> execute or die " unable to execute query ";

 
Help 
Jim f



Re: I have numbers in an array how do extract them in a random order ?

2002-04-10 Thread Felix Geerinckx

on Wed, 10 Apr 2002 19:21:11 GMT, Jim-Cont Flaherty wrote:

> I have numbers in an array how do extract them in a random order ? .
> I want to feed them to a sql query to extract information , just the
> first 10 of them 

This is a frequently asked question:

perldoc -q random
How do I select a random element from an array?
How do I shuffle an array randomly?

But you can also retrieve a random record from a table with the following 
SQL statement:

SELECT * FROM table ORDER BY RAND() LIMIT 1

-- 
felix

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]