TroyH has asked for the wisdom of the Perl Monks concerning the following question:
I have problem with comparing two strings, one is generated from a random “if else” it's called $answer, the other is input from a form, it called $useranswe. If I test to see if there the same it fails but if I say $answer = “HsRtm” then compare it to $useranswer it works.
1. $randomNumber = int(rand(100)) +1; if ($randomNumber < 100 and $randomNumber > 50) { $answer = "kbNtA"; } else { $answer = "WiaNq"; } 2. $answer = “qUfuT”;
1. doesn't work but 2. does.
Can anyone explain to me why does the random statement corrupt the $answer
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Random F%^k up strings!
by marinersk (Priest) on May 21, 2017 at 04:08 UTC | |
I'd lay good odds that randis not actually messing up your strings. You are probably making a mistake in the declaration of your variables. Let's walk through the process. I added one line to your code so we could see the results:
It seems to be working fine:
However, you're not running strictand warnings, which are critical to avoid mistakes and many bad programming techniques. So I cleaned up the code a bit:
That doesn't work so well, and the issues raised could be the source of your problem. (It's hard to tell since you clearly did not show enough code and data to reproduce the problem.)
So I cleaned up the errors:
And it seems to be working again:
I'd suggest using strictand warningsand declaring your variables with myand then re-run the script. I would not be surprised if Perl tells you what you're doing wrong. | [reply] [d/l] [select] |
by TroyH (Initiate) on May 22, 2017 at 00:29 UTC | |
| [reply] [d/l] |
by huck (Prior) on May 22, 2017 at 01:18 UTC | |
You should have used <code> and </code> to delimit your code. it makes it much easier to read Despite that i dont think you understand the nature of how cgi works. Each time that runs because the user pressed verify it generates a new $answer, that bears no relationship to whatever was typed into $useranswer the previous time it was displayed to the user. This is what you test against the users answer so it is bound not to match this run. I dont do capcha stuff much, but i would include a hidden string to return in the post. This hidden string would have been used to create the previous "$answer" in a way that the end user would not be able to replicate easily. When this parameter is found in the reply you would then use it to regenerate the previous answer and compare it to what was returned in $useranwser. I might pick CGI::Session as the mechanism to store the previous answer and have the sessionid be used as the returned value to regenerate the $answer. Edit: add example code
| [reply] [d/l] [select] |
by TroyH (Initiate) on May 22, 2017 at 17:36 UTC | |
by huck (Prior) on May 22, 2017 at 18:14 UTC | |
|
Re: Random F%^k up strings!
by tobyink (Canon) on May 21, 2017 at 09:10 UTC | |
Okay.
Well, there's your problem. $useranswe and $useranswer are two different variable names. … Now, I know what you're thinking. I spelt the variable name wrong when posting on PerlMonks. It's correct in my code. But how do we know that? You haven't shown us the code where you read the user input. You haven't shown us the code where you do the string comparison. | [reply] [d/l] [select] |
|
Re: Random F%^k up strings!
by derby (Abbot) on May 20, 2017 at 23:47 UTC | |
Hi TroyH and welcome to the monastery. Your question is a bit vague and does not really give enough info to give a proper answer (see How do I post a question effectively?). Is the form a web form or some type of console input. If console, I'd hazard a guess that the input has a trailing newline that needs to be chomped. Other than that, I think you'd need to reduce your comparison code to a simple test case and post it back here ... could be a problem with your comparison (ahh ... the many times I used = instead of == or eq).
-derby
| [reply] |
|
Re: Random F%^k up strings!
by AnomalousMonk (Archbishop) on May 21, 2017 at 00:27 UTC | |
This doesn't speak to your problem, but FWIW there's a neat way to equally select at random from an array, as you seem to be doing: (Of course, the rand built-in isn't all that random, but that's another tale for another thread.) Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |
|
Re: Random F%^k up strings!
by dsheroh (Monsignor) on May 21, 2017 at 08:29 UTC | |
Can anyone explain to me why does the random statement corrupt the $answerIt doesn't. Your problem lies in some of the code that you haven't shown us. At a guess, my money is on the comparison itself, based on where I've seen other people report similar problems. Can you pare your code down to a minimal (don't just post the whole program!), but complete, stand-alone example that we can run for ourselves which demonstrates the problem? That would allow us to answer definitively (because we can run your code, see it work incorrectly, then fix it, run it again, and see it work correctly), plus there's a very strong chance that you'll find the answer yourself in the process of trying to create the minimal example program. | [reply] |