脚本的目的是处理文件中的所有单词,并输出出现次数最多的所有单词。所以如果有三个单词出现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;) –