Quote:
“ |
Originally Posted by Davo1977
This is what I have done so far on the following subject could anybody ellaborate on it please.
|
” |
Well - it seems a fairly good start. However, the way it reads suggests that it has been copied from two (at least) different places. Do you actually understand the code? Because if you do some of the changes should be obvious.
I'll comment on some of the things I spotted. That isn't to say that there may not be other points:
Quote:
“ |
Originally Posted by Davo1977
#check if filename is valid, exit if not
if ($filename !~ m/[a-zA-Z]{0,7}\.TXT$/i)
|
” |
If you are using /i then the a-zA-Z is redundant - just use a-z. (Strictly you should use [:alpha:] instead of [a-z], but I wouldn't ding a beginner for that!)
You are insisting on the .TXT being there - not allowing it to be optional.
You aren't anchoring the match to the beginning of the filename (use ^).
You allow a zero length part before the . - I would say that was a grey area - I'd insist on at least one character myself.
Quote:
“ |
Originally Posted by Davo1977
my $file = "data1.txt";
|
” |
You have got your filename above - why introduce something else?
Quote:
“ |
Originally Posted by Davo1977
open(READFILE, "<$data1.txt") or die "Can't open file '$data1.txt': $!";
|
” |
This is muddled. data1.txt was a literal filename - not a variable. If you wanted to use a variable it would be $file (or $filename from higher up).
Quote:
“ |
Originally Posted by Davo1977
#then use a while loop and series of if statements similar to the following
while (<READFILE>) {
chomp; #removes the input record Separator
$i = $.; #"$". is the input record line numbers, $i++ will also work
$p++ if (m/^$/); #count paragraphs
$my @t = split (/\s+/); #split sentences into "words" + store them in @t
|
” |
You can't use my like this! It isn't a variable. And you use my on a variable just once, but the loop here will try and reuse it. Take the 'my @t' out of the loop.
Quote:
“ |
Originally Posted by Davo1977
$words += @t; #count all characters except spaces and add to $chars
|
” |
The comment doesn't match the code. The code is counting words.
Harry.