http://qs321.pair.com?node_id=1190780


in reply to Random F%^k up strings!

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:

$randomNumber = int(rand(100)) +1; if ($randomNumber < 100 and $randomNumber > 50) { $answer = "kbNtA"; } else { $answer = "WiaNq"; } print "Answer = [$answer]\n";

It seems to be working fine:

S:\PerlMonks>perl scope0.pl Answer = [kbNtA] S:\PerlMonks>perl scope0.pl Answer = [kbNtA] S:\PerlMonks>perl scope0.pl Answer = [kbNtA] S:\PerlMonks>perl scope0.pl Answer = [kbNtA] S:\PerlMonks>perl scope0.pl Answer = [WiaNq]

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:

#!/usr/bin/perl use strict; use warnings; $randomNumber = int(rand(100)) +1; if ($randomNumber < 100 and $randomNumber > 50) { $answer = "kbNtA"; } else { $answer = "WiaNq"; } print "Answer = [$answer]\n";

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.)

S:\PerlMonks>perl scope2.pl Global symbol "$randomNumber" requires explicit package name at scope2 +.pl line 5. Global symbol "$randomNumber" requires explicit package name at scope2 +.pl line 6. Global symbol "$randomNumber" requires explicit package name at scope2 +.pl line 6. Global symbol "$answer" requires explicit package name at scope2.pl li +ne 7. Global symbol "$answer" requires explicit package name at scope2.pl li +ne 9. Global symbol "$answer" requires explicit package name at scope2.pl li +ne 11. Execution of scope2.pl aborted due to compilation errors. S:\PerlMonks>

So I cleaned up the errors:

#!/usr/bin/perl use strict; use warnings; my $answer = ''; my $randomNumber = int(rand(100)) +1; if ($randomNumber < 100 and $randomNumber > 50) { $answer = "kbNtA"; } else { $answer = "WiaNq"; } print "Answer = [$answer]\n";

And it seems to be working again:

S:\Steve\Dev\PerlMonks\P-2017-05-20@2349-Variable-Scope-Failure>perl s +cope3.pl Answer = [WiaNq] S:\Steve\Dev\PerlMonks\P-2017-05-20@2349-Variable-Scope-Failure>perl s +cope3.pl Answer = [WiaNq] S:\Steve\Dev\PerlMonks\P-2017-05-20@2349-Variable-Scope-Failure>perl s +cope3.pl Answer = [kbNtA]

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.