1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Perl

Discussion in 'CIW Certifications' started by Mix'Creations, Oct 28, 2009.

  1. Mix'Creations

    Mix'Creations Nibble Poster

    56
    0
    28
    Hi sorry to not have been around recently, been hard studying, but im stuck for the moment, would appreciate help to the following.Thank you.


    You are to write a Perl program that analyses text files to obtain statistics on their content. The program should operate as follows:

    1) When run, the program should check if an argument has been provided. If not, the program should prompt for, and accept input of, a filename from the keyboard.

    I will help you with point one to get the ball rolling. You should write something similar to the following;

    ###Page 8-4 - command line arguments###

    ###point 1)###
    if ($#ARGV == -1) # no filename provided as a command line argument
    {
    print(\"Enter filename: \"); #more can be added here
    $filename = <STDIN>;
    chomp($filename);
    }
    else # got a filename as an argument
    {
    $filename = $ARGV[0];
    }

    ###end code snippet###

    With this in mind lets now attempt to address points two and three.

    2) The filename, either passed as an argument or input from the keyboard, should be checked to ensure it is in MS-DOS format. The filename part should be no longer than 8 characters and must begin with a letter or underscore character followed by up to 7 letters, digits or underscore characters. The file extension should be optional, but if given is should be \".TXT\" (upper- or lowercase).

    If no extension if given, \".TXT\" should be added to the end of the filename. So, for example, if \"testfile\" is input as the filename, this should become \"testfile.TXT\". If \"input.txt\" is entered, this should remain unchanged.

    3) If the filename provided is not of the correct format, the program should display a suitable error message and end at this point.

    ###page 3-6/3-7 - character classes###

    ### point 2 and 3)###
    ### then, perform the specified checks###
    ### check if filename is valid, exit if not###
    if ($filename !~ m/ /i)
    {
    die(\"not valid.\\n\"); #more can be added here
    }

    # does the filename end with .TXT?
    if ($filename !~ m/ /i)
    {
    $filename .= \".TXT\";
    }

    ###end code snippet###

    Note how I have left blanks where patterns should be. Refer to page 3-6/3-7. Try this
    first and then try to answer points 4 & 5.

    4) The program should then check to see if the file exists using the filename provided. If the file does not exist, a suitable error message should be displayed and the program should end at this point.

    5) Next, if the file exists but the file is empty, again a suitable error message should be displayed and the program should end.

    ###Page 7-6 - Determining information about files###

    ### point 4)###
    ### check if filename is actual file, exit if not###
    if ( )
    {
    die(\" error \"); #more can be added here
    }

    ### point 5)###
    ### check if filename is empty, exit if it is###
    if ( )
    {
    die(\" error \"); #more can be added here
    }


    ###end code snippet###

    Again, note how I have left blanks for the if conditions. Refer to Page 7-6 for more
    information.


    6) The file should be read and checked to display crude statistics on the number of characters, words, lines, sentences and paragraphs that are within the file.

    I will leave the remainder of the assignment for you to complete although here is an example on how to count sentences.

    ###page 7-4 - Opening files for Reading###
    ###Page 7-5 - The getc function###
    ###Page 2-3 - String Boolean expressions###
    ###Page 2-6 - The While Statement###

    open(READFILE, \"<$filename\")
    or die \"Could not open file \\\"$filename\\\":$!\";

    $sentences = 0;

    ###you would need to declare a variable such as ###

    my($ch);

    ###then use a while loop and series of if statements similar to the following###

    while ($ch = getc(READFILE))
    {

    # count sentences:
    if ($ch eq \"?\" || $ch eq \"!\" || $ch eq \".\")
    # if character is one of the three end of sentence markers
    {
    $sentences++;
    }
    }

    close(READFILE);

    # display results
    print(\"Sentences: $sentences\\n\");

    ###end code snippet###
     
    Certifications: 1D0-510) CIW Web Foundations Associate,
    WIP: On route to Guru
  2. dazza786

    dazza786 Megabyte Poster

    758
    30
    67
    so what is it that you want help on?
    ...or do you just want us to do your homework for you?
     
    Certifications: MCP (271, 272, 270, 290, 291, 621, 681, 685), MCDST, MCTS, MCITP, MCSA, Security+, CCA(XA6.5)

Share This Page

Loading...