Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to randomize

Status
Not open for further replies.

techer

MIS
Dec 2, 2002
35
AU
Hi, there.

I'm a teacher from NY City. I have learned a very little bit of Perl. The teachers in my school love to play vocabulary BINGO with students. I'm trying to make our jobs easier by writing a simple script, but it's not so simple. I working on my knowledge of Perl, but I have hit a total roadblock.

I have written a small script that takes data (25 words) input by users from a form and then it spits out the words in the form of a table with borders to produce a bingo card. That part works fine.

Here's my dilemma, I then want a button to appear at the bottom that says something like, “Shuffle Words”. Then when the user clicks it, the script would create the same card with the words in a different order (random) within that table. Sounds simple, but I'm lost.

I'm on my fifth book that tells me anyone can write Perl, yet I still have no clue what to do. Could you please help?

A 1000 thanks!

Bob

Here is the code to the script, so far:

=====================================================================
#!/usr/bin/perl

# Decode incoming form data from browser
&parseForm;

# Print the page
&printPage;


## Begin sub routines #

# Decode and prepare form data
sub parseForm {

if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN,$buffer,$ENV{CONTENT_LENGTH});
@pairs = split(/&/, $buffer);
} else {
@pairs = split(/&/,$ENV{'QUERY_STRING'});
}

foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair, 2);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$in{$name} = $value;
}

$field1=$in{'word'};
$field2=$in{'word2'};
$field3=$in{'word3'};
$field4=$in{'word4'};
$field5=$in{'word5'};
$field6=$in{'word6'};
$field7=$in{'word7'};
$field8=$in{'word8'};
$field9=$in{'word9'};
$field10=$in{'word10'};
$field11=$in{'word11'};
$field12=$in{'word12'};
$field13=$in{'word13'};
$field14=$in{'word14'};
$field15=$in{'word15'};
$field16=$in{'word16'};
$field17=$in{'word17'};
$field18=$in{'word18'};
$field19=$in{'word19'};
$field20=$in{'word20'};
$field21=$in{'word21'};
$field22=$in{'word22'};
$field23=$in{'word23'};
$field24=$in{'word24'};
$field25=$in{'word25'};
}


# Print the page
sub printPage {
print "Content-type: text/html\n\n";

print<<&quot;HTML&quot;;
<html>
<head>
<title>Your Bingo Card Is Ready!</title>
</head>

<body bgcolor=&quot;#FFFFFF&quot;>
<table width=&quot;620&quot; border=&quot;5&quot; cellspacing=&quot;0&quot; cellpadding=&quot;0&quot; align=&quot;center&quot;>
<tr>
<td width=&quot;118&quot;>
<p align=&quot;center&quot;> </p>
<p align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field1</font></b></font></p>
<p align=&quot;center&quot;> </p>
</td>
<td width=&quot;122&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field6</font></b></font></div>
</td>
<td width=&quot;127&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field11</font></b></font></div>
</td>
<td width=&quot;120&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field16</font></b></font></div>
</td>
<td width=&quot;113&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field21</font></b></font></div>
</td>
</tr>
<tr>
<td width=&quot;118&quot;>
<p align=&quot;center&quot;> </p>
<p align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field2</font></b></font></p>
<p align=&quot;center&quot;> </p>
</td>
<td width=&quot;122&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field7</font></b></font></div>
</td>
<td width=&quot;127&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field12</font></b></font></div>
</td>
<td width=&quot;120&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field17</font></b></font></div>
</td>
<td width=&quot;113&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field22</font></b></font></div>
</td>
</tr>
<tr>
<td width=&quot;118&quot; height=&quot;24&quot;>
<p align=&quot;center&quot;> </p>
<p align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field3</font></b></font></p>
<p align=&quot;center&quot;> </p>
</td>
<td width=&quot;122&quot; height=&quot;24&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field8</font></b></font></div>
</td>
<td width=&quot;127&quot; height=&quot;24&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field13</font></b></font></div>
</td>
<td width=&quot;120&quot; height=&quot;24&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field18</font></b></font></div>
</td>
<td width=&quot;113&quot; height=&quot;24&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field23</font></b></font></div>
</td>
</tr>
<tr>
<td width=&quot;118&quot;>
<p align=&quot;center&quot;> </p>
<p align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field4</font></b></font></p>
<p align=&quot;center&quot;> </p>
</td>
<td width=&quot;122&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field9</font></b></font></div>
</td>
<td width=&quot;127&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field14</font></b></font></div>
</td>
<td width=&quot;120&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field19</font></b></font></div>
</td>
<td width=&quot;113&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field24</font></b></font></div>
</td>
</tr>
<tr>
<td width=&quot;118&quot;>
<p align=&quot;center&quot;> </p>
<p align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field5</font></b></font></p>
<p align=&quot;center&quot;> </p>
</td>
<td width=&quot;122&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field10</font></b></font></div>
</td>
<td width=&quot;127&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field15</font></b></font></div>
</td>
<td width=&quot;120&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field20</font></b></font></div>
</td>
<td width=&quot;113&quot;>
<div align=&quot;center&quot;><font size=&quot;4&quot;><b><font size=&quot;3&quot;>$field25</font></b></font></div>
</td>
</tr>
</table>
</body>
</html>
HTML

}

exit;
#END OF SCRIPT
 
here's some code to shuffle a hash.
The results of the code are the value fields of a hash stored in the odd indexes of the array.

assigning a hash to an array results in
all the keys being stored in even indexes and all
the values stored in odd index's
ex: hash{'key'}='value';
@array = %hash;
#$array[0]='key'
#$array[1]='value'


Hope this helps,

Janie


#!/usr/bin/perl
###########BEGIN PERL CODE##############
$in{'one'}=&quot;one number&quot;;
$in{'two'}=&quot;two number&quot;;
$in{'three'}=&quot;three number&quot;;
$in{'four'}=&quot;four number&quot;;
$in{'five'}=&quot;five number&quot;;
$in{'six'}=&quot;six number&quot;;
$in{'seven'}=&quot;seven number&quot;;
$in{'eight'}=&quot;eight number&quot;;
$in{'nine'}=&quot;nine number&quot;;
$in{'ten'}=&quot;ten number&quot;;
$in{'eleven'}=&quot;eleven number&quot;;
$in{'twelve'}=&quot;twelve number&quot;;
$in{'thirteen'}=&quot;thrirteen number&quot;;
$in{'fourteen'}=&quot;fourteen number&quot;;
$in{'fifteen'}=&quot;fiteen number&quot;;

#if you don't have access to command line perl interprter
#uncomment the line below to view results from your browser
#print &quot;Content-type: text/html\n\n&quot;;

@array=&shuffle;
$i=1;
while ($i<$#array){
print &quot;array index $i = $array[$i]\n&quot;;
$i=$i+2;
}
print &quot;\n new shuffling\n&quot;;
@array=&shuffle;
$i=1;
while ($i<$#array){
print &quot;array index $i = $array[$i]\n&quot;;
$i=$i+2;
}


sub shuffle{
#takes the hash and creates a new shuffled array
#uses %in as a global variable
my %hash=%in;

#assign the hash to an array
#all the keys of a hash are stored in even indexes
#all the values of a hash are stored in odd indexes
#ex: $hash{&quot;key&quot;}=&quot;value&quot; ==> @array=%hash ==> array[0]=&quot;key&quot; array[1]=&quot;value&quot;
my @unshuffledarray=%hash;
my $hashlength=$#unshuffledarray/2;
my $arraylength = $#unshuffledarray;

$i=1;
while ($i<=$arraylength){
$shuffleindex= int rand($hashlength);
$temp=$unshuffledarray[$i];
if ( $shuffleindex % 2 ==0){
#ensure that we are shuffling only the
#values and not the keys of the hash
$shuffleindex++;
}
#make the switch of the two values
$unshuffledarray[$i]=$unshuffledarray[$shuffleindex];
$unshuffledarray[$shuffleindex]=$temp;
$i=$i+2;
}

$i=1;

#now we have a shuffled array that can be printed out
return @unshuffledarray;
}




###############END OF CODE################
 
Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top