Greetings all,

I've attached my code as a text document (hope this newsgroup takes 
attachments!)

The address of the page that I'm working on is 
http://staff.washington.edu/guru/cards.cgi

The page seems like it should be pretty simple... but I'm a total newbie to 
perl.

When you check various boxes in the "Play" column and click "Update" the 
following should happen:
1) the checked cards should be removed from the "Draw Deck"
2) the checked cards should be added to "Played Cards"

The cards are removed from the "Draw Deck" just fine, but the "Played Cards" 
side is populated with the next card in the "Draw Deck" list (as opposed to 
the exact card you checked).  It's easiest to just check the first card, and 
pay attention to the second card.  The second card will show up in the 
"Played Cards" list, but the first card (the one you checked) gets removed 
from the "Draw Deck".

I'm figuring that I'm just missing some @rray thing (being 0-based and all), 
but I just can't see what I'm doing wrong.

I appreciate all the help!

-James

James Woods
Web Developer for Christ by night
WebMaster at http://www.tacoma.washington.edu by day
Web Applications and Cold Fusion Programming
[EMAIL PROTECTED]





_________________________________________________________________
MSN Photos is the easiest way to share and print your photos: 
http://photos.msn.com/support/worldwide.aspx
#!/usr/local/bin/perl
#include <stdlib.h>

use CGI qw(:standard);
use strict;

#This program needs several text files
# 1) deck.txt => The whole deck
# 2) draw.txt => The draw deck
# 3) played.txt => all cards that have been played
#
#Each time the page is "updated" (by clicking on "update" in the form:
#     1) the draw.txt file should be created anew with all the played
#        (checked) cards removed
#     2) the played.txt file should be created anew with all the played
#        (checked) cards appended
#
#
#
print "Content-type: text/html\n\n";

print qq{
<p><a href="cards.cgi">Start Over</a></p>
};

my @Deck; # The whole deck, orderd
my @Draw; # The current, shuffled deck
my @playedCardsCheckbox = param("playedCardsCheckbox") || "";
my @playedCards;
my $iterate;
my $random;
my $control;
my $counter = 0;
my $valueName;

# if the update button has NOT been pressed.
# e.g. when the page is just run for the first time.

if (param("update") eq "no" || param("update") eq undef){

unlink ("played.txt");

open (DECK,"deck.txt");
@Deck = <DECK>;
close DECK;

@Draw = @Deck;

&Randomize (\@Draw);

open (DRAW,">draw.txt") || &CgiDie ("Cannot open $!");
foreach my $item (@Draw){
print DRAW $item;
}
close DRAW;

# The else happens when the update button has been pressed.

}else{

# The DRAW file is used to keep track of the 'shuffle order'

open (DRAW,"draw.txt");
@Draw = <DRAW>;
close DRAW;

if (playedCardsCheckbox){

   #print param("playedCardsCheckbox");
     foreach my $item (@Draw){
     @Draw[param("playedCardsCheckbox")] = ("");
     }
}

open (DRAW,">draw.txt");
foreach my $item (@Draw){
   if ($item ne ""){
      print DRAW $item;
      }
}
close DRAW;

open (DRAW,"draw.txt");
@Draw = <DRAW>;
close DRAW;


# The PLAYED file is used to keep track of what cards have been
#   played.  This file populates the playedCards select box.

open (PLAYED,"played.txt");
@playedCards = <PLAYED>;
close PLAYED;

if (playedCardsCheckbox){

     print param("playedCardsCheckbox");
     foreach my $item (param("playedCardsCheckbox")){
     print @playedCards[$item],"<br>";
     push(@playedCards, "$Draw[$item]");
     }
}

open (PLAYED,">>played.txt");
foreach my $item (@playedCards){
print PLAYED "$item";
}
close PLAYED;



# end of the "update" if/else loop
}

sub Randomize
{
  $random = shift;
  for ($iterate = @$random; --$iterate; )
   {
    $control = int rand ($iterate + 1);
    if ($iterate == $control)
     { next; }
    @$random[$iterate, $control] = @$random[$control, $iterate];
   }
}


print qq|
<html>
<body>
  <form name="form" method="get" action="cards.cgi">
  <input type="hidden" name="update" value="yes">
<div style="font-size:2em;">Draw Deck</div>

<table border="1" align="left">
<tr>
<th>C#</th>
<th>L.C.D</th>
<th>Play</th>
<th>Card Name</th>
</tr>
|;
#This prints out the randomized array
#Ok, figure out how to make the @Draw list remove anything
#checked.  Also how to add whatever was removed to the
#big textarea on the right.
#The first checked element shows up in the textarea.
#Try to loop over the @array in the textarea.

foreach my $item (@Draw){

#$valueName = $counter."_".$item;
$valueName = $item;

print qq|
<tr>
<td>$counter</td>
<td><input type="radio" name="card" value="$item"></td>|;

print "<td><input type='checkbox' name='playedCardsCheckbox'
value='$counter'></td>";


print qq|
<td>$item</td>
</tr>
|;

$counter++;
}


print qq{
   </table>
<div style="font-size:2em;">Played Cards</div>
   <select name="playedCards" size="10">};

    foreach my $item (@playedCards){
    print "<option value='$item'>",$item,"</option>";
    }

print qq{
</select><br />
   <input type="submit" value="Update">

  </form>
</body>
</html>

}


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

Reply via email to