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.


In reply to Re: Random F%^k up strings! by marinersk
in thread Random F%^k up strings! by TroyH

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.