Hi,
I try to make a perl program that count words from text files and from parsed html files. Right now I just started with text files and try to count the words in them. If I specify multiples text files as arguments to my program the program count all words from all files and put them in an hash. The problem I have is that I want to be able to get statistics for each text file also if the -s option is given. For example if i run my program with: ./program -s test.txt test2.txt
Then I want to count all words in test.txt and test2.txt separately. I'm quite new to perl so any help would be appreciated a lot.
Here is my code:
I try to make a perl program that count words from text files and from parsed html files. Right now I just started with text files and try to count the words in them. If I specify multiples text files as arguments to my program the program count all words from all files and put them in an hash. The problem I have is that I want to be able to get statistics for each text file also if the -s option is given. For example if i run my program with: ./program -s test.txt test2.txt
Then I want to count all words in test.txt and test2.txt separately. I'm quite new to perl so any help would be appreciated a lot.
Here is my code:
Code:
#!/usr/local/bin/perl
use warnings;
use Getopt::Std;
use LWP::Simple;
use HTML::Parser;
getopts('is');
my @url = ();
my $pattern="[URL unfurl="true"]http://";[/URL]
foreach(@ARGV) {
if($_=~ m/$pattern/) {
push(@url,$_);
}
else {
push(@textfile,$_);
}
}
if(defined $opt_i){
foreach (@textfile) {
open(FILE,$_);
@text=<FILE>;
foreach (@text) {
@word = split(/ /,$_);
}
foreach $word (@word) {
$stat{$word}++;
}
}
foreach $stat(%stat){
print "$stat\n"; }
}
elsif(defined $opt_s) {
"make statistics for every text file by them self"
}