I have run this code as below, and I seem to get the following message on output -
Missing right curly or square bracket at TMA10.pl line 44, at end of line.
syntax error at TMA10.pl line 44, at EOF
Execution of TMA10.pl aborted due to compilation errors.
if ($#ARGV == -1)
{
print("Please Enter Filename: ");
$filename = <STDIN>;
chomp($filename);
}
else
{
$filename = $ARGV[0];
}
if ($filename !~ m/^[_a-zA-Z][^.]{1,7}\.TXT$/i)
{
die("File is not valid.\n");
}
if ($filename !~ m/\.TXT$/i)
{
$filename.=".TXT";
}
if (-e $filename)
{
die("File does not exist\n");
}
#check if filename is empty, exit if it is.
if (-s $filename)
{
die("File is empty\n");
}
open(READFILE, "<$filename") or die "Can't open file \"$filename\":$!";
$sentences = 0;
my($ch);
while($ch = getc(READFILE))
{
if ($ch eq "?" ¦¦ $ch eq "!" ¦¦ $ch eq ".")
{
$sentences++;
}
close(READFILE);
print("Sentences: $sentences\n");
while($ch = getc(READFILE))
{
if ($ch eq "?" ¦¦ $ch eq "!" ¦¦ $ch eq ".")
{
$sentences++;
}
close(READFILE);
print("Sentences: $sentences\n");
There are two left curly brackets { but only one right curly bracket }. Add the closing right curly bracket where needed.
This is a fundamental construct of perl code and most scripting lanaguages: a block starts with { and ends with } and you have to make sure to include them both.
Please Enter Filename:
I assume i write the name of the text file data1.txt which is supposed to output on the command prompt the number of sentences in the data1.txt file.
But the following message appears -
File does not exist
Am I doing this completely wrong or do I need to write the following code to output the number of sentences in the text file-
open(READFILE, "<$data1.txt") or die "Can't open file \"$data1.txt\":$!";
The perl program is named TMA10.pl and the text file is data1.txt.
I would love any comments. I fully appreciate this. As you can see im a novice to Perl and have relied on course notes to produce this code so far.
open(READFILE, "<$filename") or die "Can't open file \"$filename\":$!";
but if you pass data1.txt to the perl script, that file has to be in the same folder as your perl script to open it using only the filename or also to check it using a file test operator like -e or -s. If it is in a different folder you have to add the path to the folder as well as the filename.
Also, if you pass data1.txt to the perl script it will be changed to data1.txt.TXT instead of data1.TXT which appears to be what you are trying to do. What you want to do is just a simple substitution:
$filename =~ s/\.txt$/.TXT/i;
This looks wrong too:
if ($filename !~ m/^[_a-zA-Z][^.]{1,7}\.TXT$/i)
{
die("File is not valid.\n");
}
should be:
if ($filename !~ m/^[_a-zA-Z0-9]{1,7}\.TXT$/i)
{
die("File is not valid.\n");
}
\w is a shortcut character class for [_a-zA-Z0-9] so it can be written as:
if ($filename !~ m/^\w{1,7}\.TXT$/i)
{
die("File is not valid.\n");
}
I fully appreciate your help and have made the changes to my code, here is what happens when i output this following code. It reads -
C:\Users\david funnell\perl Assignment\TMA10.pl
Please Enter Filename: Assignment\data.txt
File is not valid.
I have tried various different outputs, for example -
Please Enter Filename; Assignment\data
File is not valid.
As you can see i have put TMA10.pl & data.txt in a folder named Assignment, but every time i run my code it just keeps reading File is not valid.
Some where along the line im writing my code completley wrong. Any suggestions please?
Thankyou very much..
Here's a reminder of the way my code is -
if ($#ARGV == -1)
{
print("Please Enter Filename: ");
$filename = <STDIN>;
chomp($filename);
}
else
{
$filename = $ARGV[0];
}
if ($filename !~ m/^[_a-zA-Z0-9]{1,7}\.TXT$/i)
{
die("File is not valid.\n");
}
if ($filename !~ m/\.TXT$/i)
{
$filename .=".TXT";
}
if (-e $filename)
{
die("File does not exist\n");
}
#check if filename is empty, exit if it is.
if (-s $filename)
{
die("File is empty\n");
}
open(READFILE, "<$filename") or die "Can't open file \"$filename\":$!";
$sentences = 0;
my($ch);
while($ch = getc(READFILE))
{
if ($ch eq "?" ¦¦ $ch eq "!" ¦¦ $ch eq ".")
{
$sentences++;
}
}
close(READFILE);
print("Sentences: $sentences\n");
if ($filename !~ m/^[_a-zA-Z0-9]{1,7}\.TXT$/i)
{
die("File is not valid.\n");
}
You can only enter a filename with one to seven characters and they can only be "_a-zA-Z0-9" followed by ".txt". If the perl file and the data file are both in the same folder you do not need to enter the folder name: "Assignment\". You must enter only "data.txt". If you enter "data" the regexp will not allow it as it does not have a .txt on the end.
This is not needed:
if ($filename !~ m/\.TXT$/i)
{
$filename .=".TXT";
}
because the regexp before that is already checking that the filename has a .txt extension on the end. Or you have to modify the first regexp and not have it check the extension so you can add it if it is not present (per your assignment requirements).
if (-e $filename)
{
die("File does not exist\n");
}
#check if filename is empty, exit if it is.
if (-s $filename)
{
die("File is empty\n");
}
They will do the oppsoite of what you want, they will die if the file exists or if the file is not zero size. You want to use the negated form of condition:
if (!-e $filename)
{
die("File does not exist\n");
}
#check if filename is empty, exit if it is.
if (!-s $filename)
{
die("File is empty\n");
}
I also doubt getc() is the correct way to count the punctuation but it might work, I'm not really sure.
#/usr/bin/perl/
use strict;
use warnings;
if ($#ARGV == -1)
{
print("Please Enter Filename: ");
$filename = <STDIN>;
print("Statistics for $filename");
chomp($filename);
}
else
{
$filename = $ARGV[0];
}
if (!-e $filename)
{
die("File does not exist\n");
}
if (!-s $filename)
{
die("File is empty\n");
}
open(READFILE, "<$filename") or die "Can't open file \"$filename\":$!";
I have one more problem to tackle -
This code should be read to display statistics such as the number of characters, words, lines, sentences and paragraphs within the txt file.
I need to include the fact that the character count includes whitespaces and punctuation characters and sentences are ended by either a full stop, question mark or exclamtion mark.
This is what i've done so far-
$sentences = 0;
my($ch); #declare a variable to test the amount of characters in the file
while($ch =(READFILE)) #while loop
{
if ($ch eq "?" ¦¦ $ch eq "!" ¦¦ $ch eq ".")
{
$sentences++;
}
}
close (READFILE);
print "There are Sentences: $sentences in $filename"
Could you give me a nudge into determining the other statistics from text file. I really am a novice and finding it extremely difficult.
while($ch =(READFILE)) #while loop
what you want to do is use the diamond operator:
while($ch =<READFILE>) #while loop
then use a combination of regexps to count the words, sentences, characters, etc.
[edited by: phranque at 11:18 pm (utc) on Aug. 5, 2008]
[edit reason] disabled smileys ;) [/edit]