Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Analysing text files to obtain statistics on their content.

davo1977davo1977 Posts: 1Member
Analysing text files to obtain statistics on their content

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.

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.



I am very new to Perl and have managed to compile this code using examples from various books. Could anyone oversee this coding and see how it could be improved.

#!/usr/bin/perl

use strict;
use warnings;

if ($#ARGV == -1) #no filename provided as a command line argument.
{
print("Please enter a filename: ");
$filename = ;
chomp($filename);
}
else #got a filename as an argument.
{
$filename = $ARGV[0];
}

#perform the specified checks
#check if filename is valid, exit if not
if ($filename !~ m^/[a-z]{1,7}.TXT$/i)
{
die("File format not valid
");)
}

if ($filename !~ m/.TXT$/i)
{
$filename .= ".TXT";
}

#check if filename is actual file, exit if it is.
if (-e $filename)
{
die("File does not exist
");
}

#check if filename is empty, exit if it is.
if (-s $filename)
{
die("File is empty
");
}

my $i = 0;
my $p = 1;
my $words = 0;
my $chars = 0;

open(READFILE, "<$data1.txt") or die "Can't open file '$filename: $!";

#then use a while loop and series of if statements similar to the following
while (<READFILE>) {
chomp; #removes the input record Separator
$i = $.; #"$". is the input record line numbers, $i++ will also work
$p++ if (m/^$/); #count paragraphs
$my @t = split (/s+/); #split sentences into "words"
$words += @t; #add count to $words
$chars += tr/ //c; #tr/ //c count all characters except spaces and add to $chars
}


#display results
print "There are $i lines in $data1
";
print "There are $p Paragraphs in $data1
";
print "There are $words in $data1
";
print "There are $chars in $data1
";

close(READFILE);

Comments

  • WEBHELP101WEBHELP101 Posts: 2Member
    Hiya,

    I am doing the very same piece of course work. Could you tell me if your piece was correct and if you managed to complete it successfully?

    Thanks.
  • AkaseAkase Posts: 2Member
    Hi
    Were you able to complete your TMA10 successfully. I am currently doing the same assignment and I'm finding it very difficult. could you give me some help and tips
  • mediastormmediastorm Posts: 2Member
    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)###
    #Provide a filename as a command line argument here such as Program:myscipt.pl#
    if ($#ARGV == -1) # no filename provided as a command line argument#
    {
    print("Enter filename: "); #more can be added here
    $filename = ;
    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)#####Add w an alpha numerical word character;equivalent to [a-zA-Z_0-9]
    {
    die("not valid.
    "); #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 ( -e $file )
    {
    die(" error "); #more can be added here
    }

    ### point 5)###
    ### check if filename is empty, exit if it is###
    if ( -z $file )
    {
    open(INOUT,+<$file")
Sign In or Register to comment.