September 25th, 2012, 01:53 PM
Hello, I am having yet one issue I really need to resolve and I assume its pretty easy, but it's been one of those days. I need to be able to count my wins and losses and I'm having such a difficult time with this. I included what I thought would work below, but if you have any reference where I went wrong please let me know.
Thanks for the help in advance.
use CGI ':standard';
print start_html(-title=>"Dice Game",
open (DATA, '>roll.txt');
open (DATA, 'roll.txt');
print" <center> roll1 $froll1--$froll2</center>";
$result="You LOSE!--<a href=http://www.sample.org/scgi-bin/dice.html>Roll Again</a>";
$result="You WIN!--<a href=http://www.sample.org/scgi-bin/dice.html>Roll Again</a>";
$result="<a href=dice.cgi>Roll Again?</a>"
Your wins $win
Your losses $loss
<form method=post action=dice.cgi >
<input type=hidden name=win value='$win' />
<input type=hidden name=loss value='$loss' />
print" <center><font face= Gabriola size=6 color=black>Dice Game<hr>
September 25th, 2012, 05:28 PM
To directly answer your question, your code doesn't do anything with the $win or $loss variables. You've defined a subroutine (called hf1) that's supposed to print out a form that records them, but it doesn't get called anywhere.
I'd have a few more general tips for you also:
- Indenting your code properly makes it much easier to read: for yourself, for anyone you collaborate with and also for us on forums who are trying to read it
- When you try printing to the roll.txt file, you forgot to print to the DATA filehandle.
- You don't declare any of your variables. In its default mode, perl will allow this, but it's bad practice. If you acidentally used variables called $froll1 and $frol11, they'd look very similar with many fonts, but they're totally different variables. A very hard bug to track down in larger programs.
- You can ask perl to force you to get into good habits by adding "use strict;" and "use warnings;" to the top of your program (just after the first line would be a good place).
- You're using the CGI module for some things but not others: better to be consistent (e.g. print header()).
- You've defined a subroutine in the middle of your code. Another thing that makes it harder to figure out what's going on. Generally, put subroutines in their own section at the end.
- You're printing the end of the HTML document (end_html) before the middle!
- There's a section in the middle that doesn't do anything! $dice1>=3; etc.
That's probably enough for now
Comments on this post
September 25th, 2012, 06:47 PM
Could you guide my way of direction to be able to collect a win/loss column?
September 25th, 2012, 08:02 PM
I haven't tried your code (I am traveling this week and have no way to test code), but I suspect that if you include the following directives in your code:
you will get from the compiler error messages and warnings pointing at a number of errors or useless constructs.
Just as an example:
appear to be completely useless. It is useful to compare two values if you make a decision based on that comparison. Here it looks like you are saying "compare and do nothing". I would think the compiler would probably warn you on this if you turn on strict and warnings pragmas.
September 25th, 2012, 08:07 PM
Thanks for recognizing that, I did see that myself and I just left it because I am editing an old code I had and I just want to touch it up.
Not sure if I should of kept it or deleted it.
September 25th, 2012, 09:37 PM
As it is, I do not see much sense to keep it, as it does nothing. Now, if these comparisons have a sense in what you are trying to do, then you need to change them and do something on the basis of the comparisons' results.
Originally Posted by CGI newb
I can't tell, since you did not really say what you want to do and how.
September 26th, 2012, 12:21 AM
The only thing that I really wanted to accomplish was to count the number of wins and losses after each match.