Hi There,
I have reviewed my code and have put it into some sort of structure.
It was to the problem that i had to:
ask for a filename
check file to see if its ms-dos file format
filename should be no longer than 8 characters and should begin with an _underscore or letter and should end with .txt not case sensitive
if not then it should add .txt
program should check whether file exists and not empty
should read the file by character and get the following statistics:
character count including whitespace the punctuation. number of words. paragraphs. lines and sentences.
output details to a separate .txt file.
#!/usr/bin/perl
print("Please enter filename: ");
$filename = <STDIN>;
chomp ($filename);
if ($filename=~m/^[_a-zA-Z][^.]{1,7}\.txt$/)
open (READFILE, $filename)|| die "Failed to open $filename: $!";
my $filecontents;
{
local undef $/;
$filecontents = <READFILE>;
}
while (<READFILE>)
my @characters = ($filecontents =~m/\b/g);
my @words = ($filecontents =~m/\b\s/g);
my $wordcount = scalar @words;
my @paragraph =($
my @sentences = ($filecontents=~m/\.$/);
$CharCount{ $characters }++;
$wordcount{ $wordcount)++;
etc
close <READFILE>;
open(OUT, ">data1.txt") || die "data1.txt not open: $!";
output data here
close(OUT);
This is as far as I have got. Could you please elaborate on my coding further?
thankyou kindly
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.