Perl TMA 11 (SkillsTrain)

Discussion in 'CIW Certifications' started by stuPeas, May 30, 2007.

  1. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76
    I have noticed a question regarding SkillsTrain TMA11 on a number of occasions and have decided to give a hint as to how the Perl script can be written. It will contain clues (not a full written script), and pointers, so read carefully.

    Question 1

    The first bit is simple enough. The course notes explain how to get input from the keyboard and store it in a variable. The notes also explain how to open FILEHANDLES so you can write to files.

    The second bit asks you to varify that the customer information is correct. It is important to realize that this verification should be done as soon as the first input from the user is obtained. In other words, the code used for the verification of the customer reference, should be written immediately after the code that asks the user for their customer rererence, and stores this input in a variable.

    The actual verification can be achieved by comparing the variable (that stores the user input) with a regular expression, and using the resultant truth in a "while"statement (as below).
    Code:
    while ($ref !~ m/^\d{4}$/)                                          
    #Reg-ex matching only a 4 digit sequence.
      {					
        print ("!!INCORRECT, Enter exactly 4 digits and no letters.\n");   
        }
    
    Notice that the while statement only executes if the statement is false.



    You can create a situation where the user is again prompted to enter the same input (if the verification failed) by using a "named block" as below.
    Code:
    REF:{
                                                          
    print ("\nPlease enter a 4 digit reference number :");              
    my $ref=<STDIN>;				
    chomp ($ref);
    
    while ($ref !~ m/^\d{4}$/)                                          
      {					
         print ("!!INCORRECT, Enter exactly 4 digits and no letters.\n");   
       redo REF; 
      }
    }
    
    Note: The course notes explain the use of the "redo" statement.


    This process can be used for all of the user input by altering the regular expression used.

    NOTE: This is only ONE WAY of doing it. It may not be the best, but at the time I used it, It was the best I could come up with.
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.