PERL Tutor Marked Assignemt 10 Section 2 - Part B

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.

May you take a look at the code i have done so far
in a file named "TMA10.pl"

#accept input of, a filename from the keyboard
print ("Enter Filename: ");
$filename = <STDIN>;
chomp ($filename);
}

if ($filename !~ m/[a-zA-Z]{0-8}\.txt$)/i)
}
die ($File Format is incorrect.!\n");

}
open (READFILE, "Data.txt") || die "Couldn't open file: $!";
while (<READFILE>)
{

close(READFILE);


Im finding this assignment extremely difficult and need more help.
Im not asking for you to give me the answers but maybe nudge me in the right direction or assist me in any further information.


I have encountered this code below which outputs correctly But doesnt include any detail regarding the file.

my $file = "data1.txt";
my $i = 0;
my $p = 1;
my $words = 0;
my $chars = 0;

open(READFILE, "$file") || die "Can't open file '$file': $!";

while (<READFILE>) {
chomp; # removes the input record seperator
$i = $.; # "$." is the input record line number. $i++ will also work
$p++ if (m/^$/); # count paragraphs
my @t = split(/\s+/); # split sentences into "words" and store them in @t
$words += @t; # add count to $words;
$chars += tr/ //c; # count all characters except spaces and add to $chars

}
print "There are $i Lines in $file\n";
print "There are $p Paragraphs in $file\n";
print "There are $words Words in $file\n";
print "There are $chars Characters in $file\n";

close(READFILE);

You're not alone I am also doing the same assignment and finding it very difficult, feeling like I have been thrown into the deep end, when I am just a perl novice, Worst I am in Africa, the internet connect here is preeeeety bad, will try to submit my ideas Hope we will make it - all the best mate:'(

i will attach my code so far which works and indicates i have two sentences in my txt file.

if ($#ARGV == -1)
{
print("Please Enter Filename: ");
$filename = <STDIN>;
print("Statistics for $filename");
chomp($filename);
}
else
{
$filename = $ARGV[0];
}
#if ($filename !~ m/^[_a-zA-Z0-9]{1,7}\.TXT$/i)
#\w is a shortcut character class for [_a-zA-Z0-9] so it can be written as:
if ($filename !~ m/^\w{1,7}\.TXT$/i)
{
die("File is not valid.\n");
}
#if (-e $filename)
#{
#die("File does not exist\n");
#}

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

#They will do the oppsoite of what you want, they will
#die if the file exists or if the file is not zero size.
#You want to use the negated form of condition:

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>)
{

$sentences++ if(m/^$/); #^ Matches the beginning of a string & $ Matches the end of a string

print "There are Sentences: $sentences in $filename\n";
close(READFILE);


This works!!

I need further help in code which tells me how to display the amount of paragraphs, lines, words and characters. I believe i need code to ensure the output includes any whitespace and punctuation characters including full stops, question marks and exclamation marks.

Does this code answer the question?

if ($ch eq "?" || $ch eq "!" || $ch eq ".")
print("characters: $characters\n");

Do i need to declare a variable my characters.

Any suggestions please..

I have already suggested what you need to do on the other forum, use some regexps. You have to show some effort at writing the regexps before I will continue to post further suggestions.

im a perl novice i am asking for a bit of code to push me in the right direction.

Hi,

Does anyone know how to complete part 6 of this?

I can count the characters, and sentences, but struggling with "words"?

Also, I suspect I may need to use a subroutine maybe?

Any advice, or the code would be helpful?

Cheers

Hello guys!
I'm doing the same assignment and believe it or not I'm hating it so much coz I don't understand it all but I'm still trying my best to complete it . I'VE COMPLETED the parts below but still found out some errors in it so please can anyone help? I am just a Perl novice so please help...

When I attempted to run the code I got the following errors.

Global symbol "$filename" requires explicit package name at zia.pl line 9.

Global symbol "$filename" requires explicit package name at zia.pl line 10.

Global symbol "$filename" requires explicit package name at zia.pl line 11.

Global symbol "$filename" requires explicit package name at zia.pl line 15.

Global symbol "$filename" requires explicit package name at zia.pl line 17.

Global symbol "$filename" requires explicit package name at zia.pl line 21.

Global symbol "$filename" requires explicit package name at zia.pl line 25.

Global symbol "$filename" requires explicit package name at zia.pl line 25.

Global symbol "$sentences" requires explicit package name at zia.pl line 28.

Unrecognized character \xA6 in column 16 at zia.pl line 34.

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 (!-z $filename)

{

die("File is empty\n");

}

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

The above works fine, however the 2nd section where you check the details of the file and returns it’s statistics is incomplete.

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.

Edited 6 Years Ago by Nick Evan: Added code-tags

Before the $filename = <STDIN>; statement you need the following statement: my $filename; to declare the $filename variable. That should stop the "Global symbol "$filename" requires explicit package name..." warnings.

Can you help me to check the rest of my code please.
I have my TMA10.pl and data.txt file in assigniment folder.

#/usr/bin/perl/ 
use strict; 
use warnings; 


if ($#ARGV == -1) 
{ 
print("my $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\":$!"; 


$sentences = 0; 

my($ch); 

while($ch =<READFILE>) #while loop  
{ 
if ($ch eq "?" ¦¦ $ch eq "!" ¦¦ $ch eq ".") 
{ 
$sentences++; 
} 

close(READFILE); 

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

The above works fine, however the 2nd section where you check the details of the file and returns it’s statistics is incomplete.

Edited 6 Years Ago by Nick Evan: Added code-tags

The above works fine? Not for me. You don't have code tags around your code and it looks like there is a smiley in there. Plus this line, if ($ch eq "?" ¦¦ $ch eq "!" ¦¦ $ch eq ".") doesn't look right, but maybe it is because your code doesn't display correctly because it is not preceded by code tags, which would allow it to display correctly.

Also, it looks like your loop reads each entire line into $ch. So if $ch eq 'Is not this a sentence?' then $ch is not equal to '?' (because it is equal to much more than '?') and will not be counted as a sentence. I think there are more errors which I don't have time to look for, such as using double quotes when you need single quotes to print a variable name without interpolating it.

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.