User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 363,827 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,240 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser:
Views: 553 | Replies: 5
Reply
Join Date: May 2008
Posts: 3
Reputation: designblocks is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
designblocks designblocks is offline Offline
Newbie Poster

PERL Text Statistic Help

  #1  
May 15th, 2008
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!
Attached Files
File Type: txt perl program.txt (302 Bytes, 4 views)
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2006
Posts: 550
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 30
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Posting Pro

Re: PERL Text Statistic Help

  #2  
May 15th, 2008
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.
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: designblocks is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
designblocks designblocks is offline Offline
Newbie Poster

Re: PERL Text Statistic Help

  #3  
May 16th, 2008
Originally Posted by KevinADC View Post
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");
}

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

close(READFILE);

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

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!
Reply With Quote  
Join Date: Jan 2006
Posts: 215
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 19
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: PERL Text Statistic Help

  #4  
May 19th, 2008
I assume READFILE is valid file handle. Because the above code does not show how you are opening file or what is READFILE.
  1. while ($ch = getc(READFILE))
  2. {
  3. ++$character_count;
  4.  
  5. if ($ch eq "?" || $ch eq "!" || $ch eq ".")
  6. {
  7. $sentences++;
  8. }
  9.  
  10. $word++; if($ch =~ /[ \t]+/);
  11. }

katharnakh.
challenge the limits
Reply With Quote  
Join Date: May 2008
Posts: 3
Reputation: designblocks is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
designblocks designblocks is offline Offline
Newbie Poster

Re: PERL Text Statistic Help

  #5  
May 19th, 2008
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
Reply With Quote  
Join Date: Mar 2006
Posts: 550
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 30
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Posting Pro

Re: PERL Text Statistic Help

  #6  
May 19th, 2008
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Perl Marketplace
Thread Tools Display Modes

Other Threads in the Perl Forum

All times are GMT -4. The time now is 12:06 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC