in reply to Re: Statistics from a txtfile
in thread Statistics from a txtfile
#!/usr/bin/perl if ($#ARGV == -1) { print("Please enter filename: "); $filename = <STDIN>; chomp ($filename); } else { $filename = $ARGV[0]; } if ($filename -r && $filename=~m/^[_a-zA-Z]/) #if filename is readable + AND matches..... { open (READFILE, $filename)|| die "Failed to open $filename: $!"; } if ($filename !~ m/\.txt$/i) #if filename does not end with .txt then +add to filename { $filename .= ".TXT"; } my $filecontents; { local undef $/; $filecontents = <READFILE>; } close <READFILE>; #slurps the whole file into a variable $sentences = 0; my @characters = $filecontents =~ m/./g; # puts a copy of each match + into @characters my($ch); my $CharCount = scalar @characters; # this counts the number of + elements in @characters my @words = $filecontents =~m/[a-zA-Z]\s/g;# matches a char followed b +y a white space character globally 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++; } } while ($ch = getc(READFILE)) { $CharCount { $ch }++; } for (@characters) { print "There are $_ \n characters"; } print "There are $sentences sentences"; + # output data # open(OUT, ">data1.txt") || die "data1.txt not open: $!"; # close(OUT);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Statistics from a txtfile
by ww (Archbishop) on Jan 07, 2008 at 22:37 UTC |