in reply to Validating User Input

Well, it'll have a newline until you chomp it, so the answer is always "never". {grin} After chomping though:
print "Enter words separated by commas."; chomp(my $answer=<STDIN>); die "Invalid input" if $answer =~ tr/a-zA-Z,//c;

-- Randal L. Schwartz, Perl hacker

Replies are listed 'Best First'.
Re: •Re: Validating User Input
by DamnDirtyApe (Curate) on Jun 11, 2002 at 06:08 UTC
    Interesting, what occured to me first was /^[a-zA-Z,]+$/. However, I'm guessing there's a reason you used tr. Mind if I ask why?
    _______________
    D a m n D i r t y A p e
    Home Node | Email

        A ha! A small piece of regexp enlightenment, and a nifty trick with tr//c. Thanks merlyn! I see from my test cases that your pattern also matches an empty string... is there a way to handle that in the tr regexp, or would it be best to say something like <code>die if ( $answer =~ tr/a-zA-Z,//c or length $answer = 0 )<code>?


        _______________
        D a m n D i r t y A p e
        Home Node | Email