50 users online (0 members and 50 guests)  


  Results 1 to 4 of 4

Related

  1. Replies: 5
  2. Matching Case    Forum: PHP Forum
    Replies: 2
  3. Matching definite articles etc    Forum: CGI Perl Forum
    Replies: 0
  4. Pattern Matching    Forum: CGI Perl Forum
    Replies: 1
  1. #1
    voodz's Avatar
    New User

    Status
    Offline
    Join Date
    Jul 2006
    Posts
    2

    Simple Pattern Matching help

    I am very new to Perl and I would like some help to what is I am sure a very simple question.

    I need to write a script that will match 4 digits exactly, no more no less. I have so far written the code below which works to an extent but it will allow more than four digts, although it won't except less which is only half way there. How do I stop it from excepting more than four digits?

    print ("Enter your four digit reference number:");
    $ref_numb=<STDIN>;
    chomp ($ref_numb);

    while ($ref_numb !~ m/\d{4}/)
    {
    print ("Invalid number! Please enter your reference number again:");
    $ref_numb=<STDIN>;
    chomp ($ref_numb);
    }

    Help would be very appreciated as it is driving me up the wall. I have tried changing the pattern, removing chomp etc. As I understand {4} should match exactly 4 times. Where am I going wrong?

    Voodz

  2. #2
    uthus's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Feb 2006
    Location
    Ft Hood
    Posts
    75

    Re: Simple Pattern Matching help

    Quote Originally Posted by voodz
    I am very new to Perl and I would like some help to what is I am sure a very simple question.

    I need to write a script that will match 4 digits exactly, no more no less. I have so far written the code below which works to an extent but it will allow more than four digts, although it won't except less which is only half way there. How do I stop it from excepting more than four digits?
    Hi voodz,

    Welcome to the forum.

    Your script will accept >4 and <4 digits. I whipped this up quickly. It's ugly, but you'll get the idea.
    Code:
    #!/usr/bin/perl
    # test.pl
    
    print ("Enter your four digit reference number:");
    $ref_numb=<STDIN>;
    chomp ($ref_numb);
    # The next three lines are so I can see if <STDIN> is acting correctly.
    print "\nYour number is $ref_numb\n\n";
    print length($ref_numb) . " digits.\n\n";
    print "The preceeding lines were printed before testing.\n\n";
    
    #while ($ref_numb !~ m/\d{4}/)
    #{
    #print ("Invalid number! Please enter your reference number again:");
    #$ref_numb=<STDIN>;
    #chomp ($ref_numb);
    #}
    
    if (length($ref_numb)!=4) {
        print "Invalid number length.\nTry again.\n\n";
    } else {
        print "Number length is " . length($ref_numb) . " digits.\n";
        print "Number is " . $ref_numb . ".\n\n";
    }
    A result for 3 digits:
    Code:
    uthus@myhost:~/perl$ perl test.pl
    Enter your four digit reference number:123
    
    Your number is 123
    
    3 digits.
    
    The preceeding lines were printed before testing.
    
    Invalid number length.
    Try again.
    
    uthus@myhost:~/perl$
    For 4 digits:
    Code:
    uthus@myhost:~/perl$ perl test.pl
    Enter your four digit reference number:1234
    
    Your number is 1234
    
    4 digits.
    
    The preceeding lines were printed before testing.
    
    Number length is 4 digits.
    Number is 1234.
    
    uthus@myhost:~/perl$
    5 digits:
    Code:
    uthus@myhost:~/perl$ perl test.pl
    Enter your four digit reference number:12345
    
    Your number is 12345
    
    5 digits.
    
    The preceeding lines were printed before testing.
    
    Invalid number length.
    Try again.
    
    uthus@myhost:~/perl$
    My only suggestion to you is to not overthink your problem. This wasn't matching as much as a string length determination.

    hth

    U
    Last edited by uthus; 07-25-2006 at 09:19 PM.

  3. #3
    jkk's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2006
    Posts
    8

    Thumbs up Re: Simple Pattern Matching help

    Just change your while to this:
    while($ref_numb !~ /\d{4,4}/)
    * 4 alone states at least 4 digits
    * 4,4 states at least 4 digits, not to exceed 4 digits.
    Good luck!

  4. #4
    jkk's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2006
    Posts
    8

    Re: Simple Pattern Matching help

    Forgot this:
    while($ref_numb !~ /^\d{4,4}$/)
    ^ (beginning of line) all the way to $ (EOL) must contain 4 digits.
    Without using some sort of boundary markers, anytime you enter 4 or more digits it will pick it up.
    Make sense???



Tags for this Thread