I have been working on an assignment for a while now and I have had many problems making the program work from the information and help supplied by my course.

The program I need to make is one that analyses text files to obtain statistics on their content.

The following rules apply:

1) When run, the program should check if an argument has been provided. If not, the program should prompt for, and accept input for, 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 it should be ".TXT" (upper or lowercase). For example:

If no extension given .TXT should be added to the end of the filename.

3) If filename provided is not correct format an error message message should appear and the program exits.

4) Program should check to see if filename supplied exists and if not error message displays and program exits.

5) If file exists but file is empty (i.e. no data) an error message must display and program exits.

6) The file must be read and checked to display crude statistics on the number of:

  1. Characters
  2. Words
  3. Lines
  4. Sentences
  5. Paragraphs

For a non perl programmer like myself I have managed to code some of the program to a satisfactory level, see below:

#usr/bin/perl

if ($#ARGV == -1)
{
print("Please enter a filename: ");
$filename = <STDIN>;
chomp($filename);
}
else
{
$filename = $ARGV[0];
}


if ($filename !~ m/[a-zA-Z]{0,7}\.TXT$/i)
{
die("File format not valid\n");)
}


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

(See attached file for the perl code I have made so far, if the above is unreadable.)

I really need help. I have made a hard effort to get this far and so want to finish this as I never give up on anything!!

Thanks!

Recommended Answers

All 6 Replies

Be more specific, what part do you need help with? It looks like you have the general idea of how to do the assignment although there might be better ways to code it, that is not important right now. Learning is incremental, you will learn better methods as you go.

What is unfortunate is that that you are not using "strict" or "warnings", so the quality of your perl course is in question.

Be more specific, what part do you need help with? It looks like you have the general idea of how to do the assignment although there might be better ways to code it, that is not important right now. Learning is incremental, you will learn better methods as you go.

What is unfortunate is that that you are not using "strict" or "warnings", so the quality of your perl course is in question.

It's not a perl course but you are right my course is only basic so I'm not to fused really. The code has been updated to the following:

#usr/bin/perl

if ($#ARGV == -1)
{
print("Please enter a filename: ");
$filename = <STDIN>;
chomp($filename);
}
else
{
$filename = $ARGV[0];
}


if ($filename !~ m/[a-zA-Z]{0,7}\.TXT$/i)
{
die("File format not valid\n");)
}


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

if (-e $filename)
{
die("File does not exist\n");
}

if (-s $filename)
{
die("File is empty\n");
}

[B]while ($ch = getc(READFILE))
{
if ($ch eq "?" || $ch eq "!" || $ch eq ".")
{
 $sentences++;
}
}

close(READFILE);

print("Sentences:    $sentences\n");[/B]

I only need help with the part of code in bold type. I now need to display statistics about the number of words, characters and paragraphs.

I think I need a separate while statement for each of the additional code for this script to be complete.

PLEASE HELP WITH:

  1. How do I get the program to understand that I need it to count every character, including white spaces?
  2. How can I get it to count the number of individual words?
  3. How do I get program to count the paragraphs?

I know a \n means a new line and that the last item on the above list uses this to count paragraphs but how do I code that?

Really getting a brain ache!

I assume READFILE is valid file handle. Because the above code does not show how you are opening file or what is READFILE.

while ($ch = getc(READFILE))
{
	++$character_count;
	
	if ($ch eq "?" || $ch eq "!" || $ch eq ".")
	{
	 $sentences++;	 
	}
	
	$word++; if($ch =~ /[ \t]+/);
}

katharnakh.

Oh thank you very much. This should be more than good, i hope, for my tutors. However I best check with them first.

I'll mark as solved if they agree it is good.
Again thank you to all contributors that have helped!

Oliver

I don't think you want to be using getc(). I'd just slurp the file or process it line by line. You can use the length() function to count all characters in a string or line. Use split() to try and count words, and maybe split to also count paragraphs.

I have been working on an assignment for a while now and I have had many problems making the program work from the information and help supplied by my course.

The program I need to make is one that analyses text files to obtain statistics on their content.

The following rules apply:

1) When run, the program should check if an argument has been provided. If not, the program should prompt for, and accept input for, 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 it should be ".TXT" (upper or lowercase). For example:

If no extension given .TXT should be added to the end of the filename.

3) If filename provided is not correct format an error message message should appear and the program exits.

4) Program should check to see if filename supplied exists and if not error message displays and program exits.

5) If file exists but file is empty (i.e. no data) an error message must display and program exits.

6) The file must be read and checked to display crude statistics on the number of:

  1. Characters
  2. Words
  3. Lines
  4. Sentences
  5. Paragraphs

For a non perl programmer like myself I have managed to code some of the program to a satisfactory level, see below:

#usr/bin/perl

if ($#ARGV == -1)
{
print("Please enter a filename: ");
$filename = <STDIN>;
chomp($filename);
}
else
{
$filename = $ARGV[0];
}


if ($filename !~ m/[a-zA-Z]{0,7}\.TXT$/i)
{
die("File format not valid\n");)
}


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

(See attached file for the perl code I have made so far, if the above is unreadable.)

I really need help. I have made a hard effort to get this far and so want to finish this as I never give up on anything!!

Thanks!

Hi!
I am currently also doing TMA10 and I am finding it difficult, this is just to ask you whether you were able to complete it and how did you do it. Could you send me some the scripts on TMA10

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.