腳本的目的是處理文件中的所有單詞,並輸出出現次數最多的所有單詞。所以如果有三個單詞出現10次,程序應該輸出所有的單詞。Perl腳本問題
腳本現在運行,這要歸功於我在這裏得到的一些提示。但是,它不處理大型文本文件(即新約)。我不確定這是我的錯,還是隻是代碼的限制。我相信該計劃還有其他一些問題,所以任何幫助將不勝感激。
#!/usr/bin/perl -w
require 5.10.0;
print "Your file: " . $ARGV[0] . "\n";
#Make sure there is only one argument
if ($#ARGV == 0){
#Make sure the argument is actually a file
if (-f $ARGV[0]){
%wordHash =(); #New hash to match words with word counts
$file=$ARGV[0]; #Stores value of argument
open(FILE, $file) or die "File not opened correctly.";
#Process through each line of the file
while (<FILE>){
chomp;
#Delimits on any non-alphanumeric
@words=split(/[^a-zA-Z0-9]/,$_);
$wordSize = @words;
#Put all words to lowercase, removes case sensitivty
for($x=0; $x<$wordSize; $x++){
$words[$x]=lc($words[$x]);
}
#Puts each occurence of word into hash
foreach $word(@words){
$wordHash{$word}++;
}
}
close FILE;
#$wordHash{$b} <=> $wordHash{$a};
$wordList="";
$max=0;
while (($key, $value) = each(%wordHash)){
if($value>$max){
$max=$value;
}
}
while (($key, $value) = each(%wordHash)){
if($value==$max && $key ne "s"){
$wordList.=" " . $key;
}
}
#Print solution
print "The following words occur the most (" . $max . " times): " . $wordList . "\n";
}
else {
print "Error. Your argument is not a file.\n";
}
}
else {
print "Error. Use exactly one argument.\n";
}
請使用編譯腳本中的 –
考慮HTTP「使用嚴格」:// WWW .66clouds.com/new_testament.html;) –