in reply to String validation

Shouldn't that regex also match upper-case letters? Your regex will say that the string "A32g" is bad, but according to the problem description, the original poster wanted to match *any* letters, including (presumably) upper-case. So that string should match. So I would think you could just change that code to:
if ($string =~ m!(^[A-Za-z0-9]{4})$!) { $string =~ tr/[a-z]/[A-Z]/; # or $string = uc $string; # do something } else { # do some error }
I did a bit of benchmarking of uc vs. tr, and the results were pretty similar:
Benchmark: timing 1000000 iterations of tr, uc... tr: 10 secs ( 7.49 usr 0.00 sys = 7.49 cpu) uc: 9 secs ( 6.60 usr 0.00 sys = 6.60 cpu)
So they should be pretty much interchangeable, in terms of performance.

Replies are listed 'Best First'.
You're right...
by chromatic (Archbishop) on Mar 25, 2000 at 02:25 UTC
    Let's make it a little more readable, then:
    if ($string =~ m!^(\w{4})$!) { $string =~ tr/[a-z]/[A-Z]/; # or $string = uc $string; # do something } else { # do some error }
    Since the OP wants four alphanumerics (and they are merely *likely* to arrive in lowercase), we'll use \w.

    The reason I suggested using tr/// instead of uc is that, in my testing, uc() failed on a scalar starting with a digit. Now it seems to work correctly. Let's go back to:

    if ($string =~ s!^(\w{4})$!uc($1)!e) { # do something } else { # do some error # print "Location: $error_url" }
      Do you mean \w? \d just matches digits. I thought of using \w, but \w is alphanumerics plus '_', and I didn't know if the OP wanted '_'.