CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Perl Lost

 



MixCreations
Novice

Nov 2, 2009, 5:15 AM

Post #1 of 17 (1544 views)
Perl Lost Can't Post

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###


FishMonger
Veteran / Moderator

Nov 2, 2009, 7:41 AM

Post #2 of 17 (1535 views)
Re: [MixCreations] Perl Lost [In reply to] Can't Post

Your instructor has already given you the structure and flow of the script.

What have you tried for the missing parts?

Show us the full script that you're testing and ask specific questions on the portions that are giving you trouble.

I can't (or won't) provide the complete solution, but I will guide you.

FYI,
Some of the code the instructor provided has syntax errors that need to be dealt with.


(This post was edited by FishMonger on Nov 2, 2009, 7:43 AM)


MixCreations
Novice

Nov 2, 2009, 8:25 AM

Post #3 of 17 (1530 views)
Re: [FishMonger] Perl Lost [In reply to] Can't Post

Sorted out.Phew hate Paerl @ mo .


FishMonger
Veteran / Moderator

Nov 2, 2009, 8:34 AM

Post #4 of 17 (1528 views)
Re: [MixCreations] Perl Lost [In reply to] Can't Post

That was quick.

Now, is the person that did your homework for you going to get the credit?


MixCreations
Novice

Nov 2, 2009, 8:38 AM

Post #5 of 17 (1526 views)
Re: [FishMonger] Perl Lost [In reply to] Can't Post

Cheeky, i get the credit for my own home work lol.

No forum has ever helped me up to now.

Lots of intensive researching does the trick.

I understand Perl has to reading it, but writing it is tough.


FishMonger
Veteran / Moderator

Nov 2, 2009, 8:55 AM

Post #6 of 17 (1524 views)
Re: [MixCreations] Perl Lost [In reply to] Can't Post

Was that assignment written by your instructor, or was it from a course book?

In either case, the person that wrote it is not very experienced in Perl.

For example, this section has syntax errors (the escaped double quotes) and is very inefficient.


Code
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);



MixCreations
Novice

Nov 2, 2009, 9:00 AM

Post #7 of 17 (1521 views)
Re: [FishMonger] Perl Lost [In reply to] Can't Post

#!c:\perl\bin\perl.exe -w

#my declares the listed variables to be local
my($filename,$chars,$words,$lines,$sentences,$paragraphs,$ch,$lastchar);

#point 1)
if ($#ARGV == -1) # no filename provided as a command line argument
{
print("Enter filename: ");
$filename = <STDIN>;
chomp($filename);
}
else # got a filename as an argument
{
$filename = $ARGV[0];
}

# point 2)
# then, perform the specified checks
# check if filename is valid, exit if not
if ($filename !~ m/[a-zA-Z]{0,7}\.TXT$/i)
{
die("\"".$filename."\" is not a valid DOS text filename\n"); # point 3)
}

# does the filename end with .TXT?
if ($filename !~ m/\.TXT$/i)
{
$filename .= ".TXT";
}

# point 4)
# check if filename is actual file, exit if not
if (!-e $filename)
{
die("The file \"".$filename."\" does not exist\n");
}

# point 5)
# check if filename is empty, exit if it is
if (-z $filename)
{
die("The file \"".$filename."\" is empty\n");
}

# point 6)
# now we can do the work:
open(READFILE, "<$filename")
or die "Could not open file \"$filename\":$!";

$chars = 0;
$words = 0;
$lines = 1; # there will always be at least one line, even if only space on it
$sentences = 0;
$paragraphs = 1; # there will always be at least one paragraph, even if only a space in it
$lastchar = '';

while ($ch = getc(READFILE))
{
$chars++; # always count the characters

# count words:
if (($ch eq "\t" || $ch eq " " || $ch eq "\n") &&
# if end of word...
($lastchar ne "\t" && $lastchar ne " " && $lastchar ne "\n"))
# ...and previous character was not an end of word
{
$words++;
}

# count sentences:
if ($ch eq "?" || $ch eq "!" || $ch eq ".")
#if character is one of the three end of sentence markers
{
$sentences++;
}

#count paragraphs and lines
if ($ch eq "\n") # if newline...
{
$lines++; # count another line
if ($lastchar eq "\n") # if last character also newline
{
$paragraphs++; # count another paragraph
}
}
$lastchar = $ch; # remember character for next time through the loop
}
# loop finished, but what if we were in a word at the end?
# check if last character not a word ending character, and if not add one to the word count
if ($lastchar ne "\t" && $lastchar ne "\n" && $lastchar ne " ")
{
$words++;
}
close(READFILE);

# display results
print("Statistics for file \"$filename\":\n");
print("Characters: $chars\n");
print("Words: $words\n");
print("Lines: $lines\n");
print("Sentences: $sentences\n");
print("Paragraphs: $paragraphs\n");


FishMonger
Veteran / Moderator

Nov 2, 2009, 9:32 AM

Post #8 of 17 (1519 views)
Re: [MixCreations] Perl Lost [In reply to] Can't Post

All Perl scripts should include these 2 pragmas

Code
use strict; 
use warnings;

It's best to use the warnings pragma instead of the -w switch.

All vars should be declared in the smallest scope that they require.


Code
if ($#ARGV == -1)

Is better written as:

Code
if ( !@ARGV )


This line doesn't fully meet the requirements.

Code
if ($filename !~ m/[a-zA-Z]{0,7}\.TXT$/i)


The instructor supplied code is using the bareword READFILE for the filehandle. Since perl 5.6 (which is almost 10 years) it has been recommended (and is better) to use a lexical var for the filehandle and the 3 arg form of open.


Code
open my $READFILE, '<', $filename 
or die "Could not open file '$filename':$!";


The while loop that process the file could be greatly improved. However, if I show you how, the instructor will know that you didn't write it. But I can suggest that you read up on the tr operator in the "Quote-Like-Operators" section of perlop.
http://perldoc.perl.org/perlop.html#Quote-Like-Operators


MixCreations
Novice

Nov 2, 2009, 10:22 AM

Post #9 of 17 (1515 views)
Re: [FishMonger] Perl Lost [In reply to] Can't Post

Hey Thanx.

Im never gonna be or want to be, a Perl exper but could come in handy.


MixCreations
Novice

Nov 2, 2009, 10:30 AM

Post #10 of 17 (1513 views)
Re: [FishMonger] Perl Lost [In reply to] Can't Post

I did actually learn a lot about what you stipulate, think there just easing us into the language quietly, so to speak so we don't get bogged down.


FishMonger
Veteran / Moderator

Nov 2, 2009, 11:10 AM

Post #11 of 17 (1512 views)
Re: [MixCreations] Perl Lost [In reply to] Can't Post


In Reply To
I did actually learn a lot about what you stipulate, think there just easing us into the language quietly, so to speak so we don't get bogged down.


That may be true, but starting out by teaching you poor or questionable coding practices is a disservice to you because it then makes it that much harder to learn good coding practices. In your case, that may not be important, but for those that do want to be good Perl programmers will suffer.


MixCreations
Novice

Nov 2, 2009, 11:14 AM

Post #12 of 17 (1508 views)
Re: [FishMonger] Perl Lost [In reply to] Can't Post

The course is an introduction to Perl only, and not meant to make you proficient at it.

Where are you from anyhow, im loving this little interaction.


FishMonger
Veteran / Moderator

Nov 2, 2009, 11:23 AM

Post #13 of 17 (1507 views)
Re: [MixCreations] Perl Lost [In reply to] Can't Post

I'm in California (Silicon Valley) and took my "Intro to Perl Programming" at Foothill college.
http://krypton.fhda.edu/~gever/


MixCreations
Novice

Nov 2, 2009, 11:27 AM

Post #14 of 17 (1504 views)
Re: [FishMonger] Perl Lost [In reply to] Can't Post

Ok great, so your an expert, must of been in the business a lot longer than i have.

I just been studying for round 2 years, a Web Development course.

I find Perl difficult, and may try my hands at something more easier, syntax i mean.

I do appreciate your help, thank you.


7stud
Enthusiast

Nov 2, 2009, 1:32 PM

Post #15 of 17 (1496 views)
Re: [FishMonger] Perl Lost [In reply to] Can't Post


In Reply To
All Perl scripts should include these 2 pragmas

Code
use strict; 
use warnings;

It's best to use the warnings pragma instead of the -w switch.

All vars should be declared in the smallest scope that they require.


Code
if ($#ARGV == -1)

Is better written as:

Code
if ( !@ARGV )


This line doesn't fully meet the requirements.

Code
if ($filename !~ m/[a-zA-Z]{0,7}\.TXT$/i)


The instructor supplied code is using the bareword READFILE for the filehandle. Since perl 5.6 (which is almost 10 years) it has been recommended (and is better) to use a lexical var for the filehandle and the 3 arg form of open.


Code
open my $READFILE, '<', $filename 
or die "Could not open file '$filename':$!";


Thanks for the tips! I've been reading "man perlintro" and it must be old. It talked about scalar variables($var), array variables (@var), and hash variables (%var), but then in the file section, suddenly it introduced INPUTFILE: a constant like variable, with no explanation.

Should the filehandle variable be capitalized($READFILE)? Also, isn't the 3 arg form of open() a pain to type? It's not considered good style to put the '<' in the same string as $filename, e.g.


Code
my $file; 
open ($file, "<data.txt") or die "....$!";



(This post was edited by 7stud on Nov 2, 2009, 1:43 PM)


7stud
Enthusiast

Nov 2, 2009, 1:35 PM

Post #16 of 17 (1493 views)
Re: [MixCreations] Perl Lost [In reply to] Can't Post


In Reply To
Ok great, so your an expert, must of been in the business a lot longer than i have.

I just been studying for round 2 years, a Web Development course.

I find Perl difficult, and may try my hands at something more easier, syntax i mean.


python would be a breath of fresh air. I don't like ruby so much, and then with ruby you probably need to learn ruby on rails for web development, which is painful. But I guess the way of the world now is to use frameworks for web development, so with python you would have to learn django or turbogears. perl, python, and ruby are competitors in the programming world.


(This post was edited by 7stud on Nov 2, 2009, 1:46 PM)


FishMonger
Veteran / Moderator

Nov 2, 2009, 1:48 PM

Post #17 of 17 (1484 views)
Re: [7stud] Perl Lost [In reply to] Can't Post

No, the filehandle does not need to be capitalized. The capitalization is a past convention when using a bareword, but even in that case, it's not required.

I normally use lowercase names and often append fh to the name to make it clear that the var is to be used as a filehandle.

Writing the 3 arg form really isn't a pain, but if you've been using barewords for years, it could take a little time to get used to the 3 arg form. The 3 arg form can be safer, especially when the filename is in a var that is a reference.

see: 'perldoc -q quoting'


(This post was edited by FishMonger on Nov 2, 2009, 1:50 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives