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

Perl Section 2B TMA 10 help ?

Discussion in 'Scripting & Programming' started by brummyneil, Apr 28, 2008.

  1. brummyneil

    brummyneil New Member

    1
    0
    1
    Hi.

    Has anyone managed to complete this perl tma, because still stuck on this and don`t understand what patterns to use as there are allways syntax errors ?, i`ve have studdied the book over an over agian but i still don`t get it all, plus it`s not explained probaly in the book :confused3

    here`s what you have to do:

    you are to write a program a 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.

    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.

    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.

    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.

    and here`s help the tutor sent me:

    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-5 - The getc function###
    ###Page 2-3 - String Boolean expressions###
    ###Page 2-6 - The While Statement###

    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###



    what ive already done: :eek:

    #!c:\perl\bin\perl.exe
    if ($#ARGV == -1)
    {
    print("Enter filename: ");
    $filename = <STDIN>;
    chomp($filename);
    }
    else
    {
    $filename = $ARGV[0];
    }
    if ($filename !~ m/\.TXT)
    die("not valid.\n");
    }if ($filename !~ m/\.txt/)
    {
    $filename .= "testfile.txt";
    }
    if (-r $filename && -w $filename)
    {
    die(" error ");
    }
    if (-e)
    {
    die(" error ");
    }
    while ($ch = getc(READFILE))
    {
    if ($ch eq "?" || $ch eq "!" || $ch eq ".")
    {
    $sentences++;
    }
    }

    close(READFILE);

    print("Sentences: $sentences\n");

    can anyone help ?:blinks
     
  2. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    Um - where to start!

    1) Check the braces - they don't match up.
    2) The first match test is missing any indication of where the match string ends.
    3) The -e operator takes a filename to operate on. But it is redundant here as you have already tested for the file being readable.
    4) You aren't opening the file, so READFILE is undefined.

    The TMAs seem to change over time, so I can only guess what the question is.

    Oh, and welcome to CF!

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  3. Davo1977

    Davo1977 New Member

    5
    0
    0
    I need to analyse a text file to obtain statistics on its contents? It should check if an argument has been provided and if not, it should prompt for, and accept input of a filename from the keyboard. This filename should also be checked to ensure it is in MS-DOS format and should be no longer than 8 characters. The file extension should be optional but if it is given then it should be .txt, either upper or lowercase. If an extension isn't given, .TXT should be added to the end of the filename. If the filename provided isn't the right format the program should display a suitable error message and end at this point. It should then check to see if the file exists using the filename given. If it doesn't then the error message should again be displayed then ended again. If the file does exist but it's empty the error message again to be displayed then ended. If the file exists and contains words or characters etc. it 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 am struggling with this assignment as i feel the course notes are simply not good enough. thompsonelvis@aol.com
     
    WIP: CIW Website Design Manager
  4. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    I've commented on this in the thread you started.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+
  5. Davo1977

    Davo1977 New Member

    5
    0
    0
    Here is some code which answers questions 1-5.
    Does anybody have any suggestions which tackles the following question 6 Below: -

    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 fie.
    - the character count can include any whitespaces and punctutaion characters (i.e. it can include all
    characters in the file)
    - each sentence is ended by either a full stop, question mark or exclamation mark.

    Code so far -

    #/usr/bin/perl/

    use strict;
    use warnings;

    if ($#ARGV == -1)
    {
    print("Please Enter Filename: ");
    $filename = <STDIN>;
    print("Statistics for $filename");
    chomp($filename);
    }
    else
    {
    $filename = $ARGV[0];
    }
    if (!-e $filename)
    {
    die("File does not exist\n");
    }
    if (!-s $filename)
    {
    die("File is empty\n");
    }
    open(READFILE, "<$filename") or die "Can't open file \"$filename\":$!";
    while(<READFILE>)
    {
     
    WIP: CIW Website Design Manager
  6. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,623
    115
    224
    Actually it misses quite a lot of the requirements of 1-5.

    Why not continue this in the thread you started?

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+

Share This Page

Loading...