943,612 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 2349
  • Perl RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 12th, 2009
0

Analysing text files to obtain statistics on their content Using Perl

Expand Post »
Hi
I'm new to Perl language and I have this assignment which is killing me, CAN SOMEONE HELP ME PLEASE. Below is the question:

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 HAVE MANAGE TO WRITE THE FOLLOWING BUT ANY TIME I EXECUTE IT I GET THE ff: Global symbol "filename" requires explicit package name form C: directory.

Below is my code, can anyone check and find out what is wrong, please:
perl Syntax (Toggle Plain Text)
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5.  
  6. if ($#ARGV == -1) #no filename provided as a command line argument.
  7. {
  8. print("Please enter a filename: ");
  9. $filename = <STDIN>;
  10. chomp($filename);
  11. }
  12. else
  13. {
  14. $filename = $ARGV[0];
  15. }
  16.  
  17. if ($filename !~ m/^[a-z]{1,7}\.TXT$/i)
  18. {
  19. die("File format not valid\n");
  20. }
  21.  
  22. if ($filename !~ m/\.TXT$/i)
  23. {
  24. $filename .= ".TXT";
  25. }
  26.  
  27. if (-e $filename)
  28. {
  29. die("File does not exist\n");
  30. }
  31.  
  32. if (-s $filename)
  33. {
  34. die("File is empty\n");
  35. }
  36.  
  37. my $i = 0;
  38. my $p = 1;
  39. my $words = 0;
  40. my $chars = 0;
  41.  
  42. open(READFILE, "<$data.txt") or die "Can't open file '$filename: $!";
  43.  
  44. while (<READFILE>) {
  45. chomp;
  46. $i = $.; #"$". is the input record line numbers, $i++ will also work
  47. $p++ if (m/^$/); #count paragraphs
  48. $my @t = split (/\s+/); #split sentences into "words"
  49. $words ++;= @t; #add count to $words
  50. $chars += tr/ //c; #tr/ //c count all characters except spaces and add to $chars
  51. }
  52.  
  53. print "There are $i lines in $data\n";
  54. print "There are $p Paragraphs in $data\n";
  55. print "There are $words in $data\n";
  56. print "There are $chars in $data \n";
  57.  
  58. close(READFILE);
Last edited by Narue; Jun 12th, 2009 at 3:55 pm. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Akase is offline Offline
3 posts
since May 2009
Jun 12th, 2009
0

Re: Analysing text files to obtain statistics on their content Using Perl

I am surprised that you got this program to run at all.

Perl Syntax (Toggle Plain Text)
  1. $my @t = split (/\s+/); #split sentences into "words"
  2. $words ++;= @t; #add count to $words

These two lines contain syntax errors.

The reason your program will not run (and you're getting the error that you're getting) is because you are using strict, but not programming in a strict fashion. You're not declaring the scope of the variable filename, which is why it is bombing. If you take strict out, you will see that you have some fairly straight forward syntax errors in your code. Take out strict and fix the basic errors. That will probably fix the code, although I'd probably take a completely different approach to this problem. I just don't have time to rewrite the entire program.
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009
Jun 12th, 2009
0

Re: Analysing text files to obtain statistics on their content Using Perl

Akase...

You insult everyone helping on this forum by posting your plagarized code and making it appear as if you wrote it.

http://www.programmersheaven.com/mb/...=B20000#392147
Last edited by KevinADC; Jun 12th, 2009 at 3:46 pm.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Jun 13th, 2009
0

Re: Analysing text files to obtain statistics on their content Using Perl

Click to Expand / Collapse  Quote originally posted by mitchems ...
I am surprised that you got this program to run at all.
Considering they did not write it, and probably never even ran it, your surprise should be alleviated.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Jun 13th, 2009
0

Re: Analysing text files to obtain statistics on their content Using Perl

Click to Expand / Collapse  Quote originally posted by mitchems ...
I am surprised that you got this program to run at all.

Perl Syntax (Toggle Plain Text)
  1. $my @t = split (/\s+/); #split sentences into "words"
  2. $words ++;= @t; #add count to $words

These two lines contain syntax errors.

The reason your program will not run (and you're getting the error that you're getting) is because you are using strict, but not programming in a strict fashion. You're not declaring the scope of the variable filename, which is why it is bombing. If you take strict out, you will see that you have some fairly straight forward syntax errors in your code. Take out strict and fix the basic errors. That will probably fix the code, although I'd probably take a completely different approach to this problem. I just don't have time to rewrite the entire program.
Hi!,
As I said earlier on I am new to Perl, I wrote the code by reading thru other peoples work and my course book. In fact I do not understand most of the syntax myself that is why I have put it online for help. I'll would be grateful if people could comment and make corrections line by line so that I can follow the steps. But it seems all the comments is so far had not helped me to add any new thin to my code. I really need help please!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Akase is offline Offline
3 posts
since May 2009
Jun 13th, 2009
0

Re: Analysing text files to obtain statistics on their content Using Perl

Why are you not asking your SkillsTraining Tutor for help? You are paying them after all.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Jun 14th, 2009
0

Re: Analysing text files to obtain statistics on their content Using Perl

Quote ...
But it seems all the comments is so far had not helped me to add any new thin to my code.
Your code? You didn't write one single thing in that code, you just copied and pasted it from the other forum. You really have no shame.
Reputation Points: 246
Solved Threads: 67
Practically a Posting Shark
KevinADC is offline Offline
898 posts
since Mar 2006
Nov 18th, 2009
1
Re: Analysing text files to obtain statistics on their content Using Perl
CAN ANYBODY HELP ME IM DOING A TMA 10 SECTION B FOR SKILLSTRAIN analyse a text file to obtain statistics on its contents ANYBODY START ME OFF THANX
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tonyfryer3 is offline Offline
1 posts
since Nov 2009
Feb 23rd, 2010
-1
Re: Analysing text files to obtain statistics on their content Using Perl
Click to Expand / Collapse  Quote originally posted by tonyfryer3 ...
CAN ANYBODY HELP ME IM DOING A TMA 10 SECTION B FOR SKILLSTRAIN analyse a text file to obtain statistics on its contents ANYBODY START ME OFF THANX
hi there
i need help with same TMA
can anybody help
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dogu is offline Offline
1 posts
since Feb 2010
Apr 7th, 2010
0
Re: Analysing text files to obtain statistics on their content Using Perl
Click to Expand / Collapse  Quote originally posted by dogu ...
hi there
i need help with same TMA
can anybody help
Hello man, Have you completed for Perl Fundamental Write Perl Program TMA? Do you need help?

Regards
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stungun is offline Offline
1 posts
since Apr 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Perl Forum Timeline: Pulling specific lines from jumbled text files
Next Thread in Perl Forum Timeline: Need to create parse tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC