I quote the variables as they may contain *@%, etc. I figured it was safer.

There's never any interpolation without quotes, and "*" and "%" are not special in string literals (although "*" is special in regex literals).

All you achieve is to force the variable to become a string it it wasn't, but functions that expect strings (such as join) will already do that.

I did not get 4 digit numbers all the time

You still didn't. You need to add the leading zeroes if you want them. That's what the sprintf is for.

I don't entirely understand the code I used, but it worked

No, it didn't. It may have seemed to work for the input you gave, but you can get PINs outside the range you want, or your PINs weren't well distributed (i.e. they were predictable), or both.

Why do you use '' as a separator in the join when it works without one?

join won't work without a separator. $data1 was being used as the separator. It's much clearer to the person reading your code (the real user of your code) if you didn't use $data1 as the separator. The fact that you're confused about the need for a separator validates my point.

I researched the modulus some but did not understand it very well,

Modulus is the remainder of an integer division. For example,

int(1234567 / 10000) = 123 1234567 % 10000 = 4567

The following holds true:

$x = int($x/$d) * $d + $x%$d

For example,

1234567 = 123 * 10000 + 4567 = int(1234567/10000) * 10000 + 1234567%10000

The key thing is that the modulus will never be bigger than the divisor.
% $d will result in a number in 0..($d-1).
By using % 65536, you could get any number in 0..65535.
By using % 10000, you'll only get numbers in 0..9999.
(sprintf '%04d' will make that 0000..9999 by adding leading zeroes.)

I needed to strip both - and \r\n from a string but never managed to do it in one search/replace

s/\r\n|-//g

If you want to remove "\r", "\n", and "-" (as opposed to "\r\n" and "-"):

s/[\r\n-]//g

You sure "\r" is actually there? "\r\n" gets transformed to "\n" automatically on a Windows machine.

s/\n|-//g
or
s/[\n-]//g

Best put - at the end of [...] or escape it ([...\-]) since it's used to do ranges ([a-z]).

perlre

Update: Added slashes missing in substitutions.


In reply to Re^5: Need a wait to generate a 4 digit "PIN" number by ikegami
in thread Need a wait to generate a 4 digit "PIN" number by jaiello

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.