in over my head, but having fun treading water
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 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{ Start Over }; 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 = ; 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 = ; 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 = ; 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 = ; close PLAYED; if (playedCardsCheckbox){ print param("playedCardsCheckbox"); foreach my $item (param("playedCardsCheckbox")){ print @playedCards[$item],""; 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| Draw Deck C# L.C.D Play Card Name |; #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| $counter |; print ""; print qq| $item |; $counter++; } print qq{ Played Cards }; foreach my $item (@playedCards){ print "",$item,""; } print qq{ } -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
help with first Perl application (repost w/ better subject line)
Greetings all, 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 #!/usr/local/bin/perl #include 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{ Start Over }; 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 = ; 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 = ; 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 = ; 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 = ; close PLAYED; if (playedCardsCheckbox){ print param("playedCardsCheckbox"); foreach my $item (param("playedCardsCheckbox")){ print @playedCards[$item],""; 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| Draw Deck C# L.C.D Play Card Name |; #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| $counter |; print ""; print qq| $item |; $counter++; } print qq{ Played Cards }; foreach my $item (@playedCards){ print "",$item,""; } print qq{ } 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] _ Send and receive Hotmail on your mobile device: http://mobile.msn.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
from file to a list containing lists
I've got a file that I would like to import into a list. The file has many rows, and each row has 3 records, seperated with a "|". Example of file is as below: f|Axe Strike|d f|Disquiet of our people|p f|Dwarven Armor|p When I was just using 1 record in the file, @Array = ended up working great. Now that I've moved on and added two more records, I just don't know what to do. I'm working with split now, but it's only returning the first row. I know you all can help this rookie out, thanks!!! _ Send and receive Hotmail on your mobile device: http://mobile.msn.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: from file to a list containing lists
OK, you're totally right, I should have printed my code 8^( I figured that it would be totally easy... then I remembered that I'm a noobie and everything is confusing 8^) First, let me say that I'm trying to get work to buy me a Perl book, but money at the state is tight these days. I've even gone to the *library* and they don't have the Perl books. 8^( Oh well, that's what the Perl community is for, right? When the line below is commented out the only thing that gets output to the browser window is the Start Over link # #push @Draw, [ split /\|/ ] while ; # Ok, the two parts that I'm having trouble with are: 1) getting the file data into an array (which you've helped me with so far)... 2) printing that array out in the table at the bottom of the code. #!/usr/local/bin/perl #include use CGI qw(:standard); use strict; print "Content-type: text/html\n\n"; print qq{ Start Over }; my @Deck; # The whole deck, orderd my @Draw; # The current, shuffled deck my @playedCards = param("playedCardsHidden") || ""; my @playedCardsCheckbox = param("playedCardsCheckbox") || ""; my @discardCards = param("discardCardsHidden") || ""; my @discardCardsCheckbox = param("discardCardsCheckbox") || ""; my $iterate; my $random; my $control; my $counter = 0; my $valueName; my $turnCounter = param("turnCounter") || 0; # 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){ open (DECK,"deck.txt"); # when the line below is commented out # the only thing that gets output to the browser window # is the Start Over link (yes, when I uncomment the one # below, I comment out the @Deck = ; line.) #push @Draw, [ split /\|/ ] while ; # this (as you well know) just gets each line in my file @Deck = ; close DECK; @Draw = @Deck; &Randomize (\@Draw); # Run this code whenever the "End turn" button is pressed }else{ $turnCounter += 1; # Perform all the actions related to # @inPlay # Perform all the actions related to # @inDiscard # Perform all the actions realted to # @Draw # 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]; } } ### # Now we start the output section # ### print qq| Draw Deck # Card Name Play Discard |; #This prints out the randomized array #my $index=0; #foreach my $item (@Draw) { #$index++; #print "List index $index:\n"; #foreach my $item (@{$item}) #{print "\t$item\n"} #} foreach my $item (@Draw){ $valueName = $item; print "\n"; print "\t$counter\n"; print "\t$item\n"; print "\t\n"; print "\t\n"; print "\n"; $counter++; } print "\n"; print qq{ In Play In Discard Cards in play are listed here Cards in the discard pile are listed here }; print qq{ } _ Join the worlds largest e-mail service with MSN Hotmail. http://www.hotmail.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: from file to a list containing lists
Ok, I'm getting closer thanks to the help of all the experts out here!! I'm totally thankful for all of your help. Now, I've rewritten most of what I had before, using suggestions and examples from our resident Perl Pros. While I'm closer to my desired outcome, I'm still not there; here is my current problem. at http://staff.washington.edu/guru/cards2.cgi when you hit "End Phase" it passes, as a hidden form field, the contents of @Draw. (I'm not even bothering to check any boxes yet.. I need to be sure that no matter what I pass the proper @Draw every time). The first time you hit "End Phase" it's great. However, subsequent times, loses the \n\r at the end of each line and gains two spaces. How should I handle this? Should I detect whether or not $Draw contains /\n\r/ and then split on that, and if not split on / /? (and if I should do that, how do I do that 8^). Or, should I make sure that I pass the same thing every time and then not have to worry about what the delimiter should be? (and if I should do it that way... uh... how do I to that? ;^) I hope that makes sense to an expert out there. Any suggestions on what I should do? I really am learning though!! I've finally got my boss to get me 'Learning Perl 3rd Ed.' and 'Effective Perl Programming'. Should pick them up tomorrow 8^) -James _ Send and receive Hotmail on your mobile device: http://mobile.msn.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: from file to a list containing lists (repost w/ code)
Duh, forgot to change the extention from .cgi to .txt Ok, I'm getting closer thanks to the help of all the experts out here!! I'm totally thankful for all of your help. Now, I've rewritten most of what I had before, using suggestions and examples from our resident Perl Pros. While I'm closer to my desired outcome, I'm still not there; here is my current problem. at http://staff.washington.edu/guru/cards2.cgi when you hit "End Phase" it passes, as a hidden form field, the contents of @Draw. (I'm not even bothering to check any boxes yet.. I need to be sure that no matter what I pass the proper @Draw every time). The first time you hit "End Phase" it's great. However, subsequent times, loses the \n\r at the end of each line and gains two spaces. How should I handle this? Should I detect whether or not $Draw contains /\n\r/ and then split on that, and if not split on / /? (and if I should do that, how do I do that 8^). Or, should I make sure that I pass the same thing every time and then not have to worry about what the delimiter should be? (and if I should do it that way... uh... how do I to that? ;^) I hope that makes sense to an expert out there. Any suggestions on what I should do? I really am learning though!! I've finally got my boss to get me 'Learning Perl 3rd Ed.' and 'Effective Perl Programming'. Should pick them up tomorrow 8^) -James -James _ Join the worlds largest e-mail service with MSN Hotmail. http://www.hotmail.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
keeping \n in an array when being passed through form
[repost with better subject and code put directly into the message] Ok, I'm getting closer thanks to the help of all the experts out here!! I'm totally thankful for all of your help. At http://staff.washington.edu/guru/cards2.cgi when you hit "End Phase" it passes, as a hidden form field, the contents of @Draw. (I'm not even bothering to check any boxes yet.. I need to be sure that no matter what I pass the proper @Draw every time). The first time you hit "End Phase" it's great. However, subsequent times, loses the \r\n at the end of each line and gains two spaces. This makes it so I can't split on the \r\n like I did before hitting "End Phase". How should I handle this? Should I detect whether or not $Draw contains /\n\r/ and then split on that, and if not split on / /? (and if I should do that, how do I do that 8^). Or, should I make sure that I pass the same thing every time and then not have to worry about what the delimiter should be? (and if I should do it that way... uh... how do I to that? ;^) I hope that makes sense to an expert out there. Any suggestions on what I should do? I really am learning though!! I've finally got my boss to get 'Learning Perl 3rd Ed.' and 'Effective Perl Programming'. Should pick them up tomorrow 8^) -James #!/usr/local/bin/perl #include use CGI qw(:standard); use strict; print "Content-type: text/html\n\n"; print qq{ Start Over }; my @Deck; # The whole deck, orderd my @Draw; my $Draw = param("drawHidden") || ""; my @playedCards = param("playedCardsHidden") || ""; my @playedCardsCheckbox = param("playedCardsCheckbox") || ""; my @discardCards = param("discardCardsHidden") || ""; my @discardCardsCheckbox = param("discardCardsCheckbox") || ""; my $iterate; my $random; my $control; my $counter = 0; my $valueName; my $turnCounter = param("turnCounter") || 0; # 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){ open (DECK,"deck.txt"); @Deck = ; close DECK; @Draw = @Deck; &Randomize (\@Draw); sub Randomize { $random = shift; for ($iterate = @$random; --$iterate; ) { $control = int rand ($iterate + 1); if ($iterate == $control) { next; } @$random[$iterate, $control] = @$random[$control, $iterate]; } } # Run this code whenever the "End turn" button is pressed }else{ ### # This works great the first time# #but fails the rest of the time # #because it loses it's \r\n # ### @Draw = split(/\r\n/ ,$Draw); print "Start Printing \@Draw "; print @Draw; print " End Printing \@Draw"; print "\@Draw has ", my $number_of_elements = @Draw, " elements"; $turnCounter += 1; # Perform all the actions related to # @inPlay # Perform all the actions related to # @inDiscard # Perform all the actions realted to # @Draw # end of the "update" if/else loop } ### # Now we start the output section # ### print qq| Draw Deck # Card Name Play Discard |; #This prints out the randomized array foreach my $item (@Draw){ my @foo = split(/\|/, $item); $valueName = $foo[1]; print "\n"; print "\t$counter\n"; print "\t$valueName\n"; print "\t\n"; print "\t\n"; print "\n"; $counter++; } print "\n"; print qq{ In Play In Discard Cards in play are listed here Cards in the discard pile are listed here }; print qq{ # here's where I'm passing @Draw # } _ MSN Photos is the easiest way to share and print your photos: http://photos.msn.com/support/worldwide.aspx -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]