
MixCreations
Novice
Nov 2, 2009, 5:15 AM
Post #1 of 17
(968 views)
|
Hi i am new and grateful to have guys like yourselves around, cause im at a complete loss with understanding this complex language, as im new to it, and only learning it as its in the sylabus of the ciw course im undertaking. I have been given the following tma to complete and i havent a scoobi doo (clue) any help would be great, but to be honest im not sure ill ever understand Perl (to write) i understand lots of aspects of it but, not enough obviously. Please help. 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)### if ($#ARGV == -1) # no filename provided as a command line argument { print(\"Enter filename: \"); #more can be added here $filename = <STDIN>; 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) { die(\"not valid.\\n\"); #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 ( ) { die(\" error \"); #more can be added here } ### point 5)### ### check if filename is empty, exit if it is### if ( ) { die(\" error \"); #more can be added here } ###end code snippet### Again, note how I have left blanks for the if conditions. Refer to Page 7-6 for more information. 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 will leave the remainder of the assignment for you to complete although here is an example on how to count sentences. ###page 7-4 - Opening files for Reading### ###Page 7-5 - The getc function### ###Page 2-3 - String Boolean expressions### ###Page 2-6 - The While Statement### open(READFILE, \"<$filename\") or die \"Could not open file \\\"$filename\\\":$!\"; $sentences = 0; ###you would need to declare a variable such as ### my($ch); ###then use a while loop and series of if statements similar to the following### while ($ch = getc(READFILE)) { # count sentences: if ($ch eq \"?\" || $ch eq \"!\" || $ch eq \".\") # if character is one of the three end of sentence markers { $sentences++; } } close(READFILE); # display results print(\"Sentences: $sentences\\n\"); ###end code snippet###
|