-2
我有一個在線的Perl一致性搜索特定文本文件中的目標詞並打印排序的輸出。測試代碼目前僅在單個文本文件中搜索關鍵字並打印輸出。但我想爲文件夾中的所有文本文件做同樣的事情,而不僅僅是一個文本文件。任何關於此的建議都會非常有用。在Perl中輸入多個.txt文件
這裏是我的網上一致性代碼:
#!/usr/bin/perl -wT
# require
use strict;
use diagnostics;
use CGI;
# sanity check
my $q = new CGI;
my $target = $q->param("keyword");
my $radius = $q->param("span");
my $ordinal = $q->param("ord");
my $width = 2*$radius;
my $file = 'DISS.G.HB.002.txt';
if (! $file or ! $target) {
print "Usage: $0 <file> <target>\n";
exit;
}
# initialize
my $count = 0;
my @lines =();
$/ = ""; # Paragraph read mode
# open the file, and process each line in it
open(FILE, " < $file") or die("Can not open $file ($!).\n");
while(<FILE>){
# re-initialize
my $extract = '';
# normalize the data
chomp;
s/\n/ /g; # Replace new lines with spaces
s/\b--\b/ -- /g; # Add spaces around dashes
# process each item if the target is found
while ($_ =~ /\b$target\w*/gi){
# find start position
my $match = $1;
my $pos = pos;
my $start = $pos - $radius - length($match);
# extract the snippets
if ($start < 0){
$extract = substr($_, 0, $width+$start+length($match));
$extract = (" " x -$start) . $extract;
}else{
$extract = substr($_, $start, $width+length($match));
my $deficit = $width+length($match) - length($extract);
if ($deficit > 0) {
$extract .= (" " x $deficit);
}
}
# add the extracted text to the list of lines, and increment
$lines[$count] = $extract;
++$count;
}
}
sub removePunctuation {
my $string = $_[0];
$string = lc($string); # Convert to lowercase
$string =~ s/[^-a-z ]//g; # Remove non-aplhabetic characters
$string =~ s/--+/ /g; #Remove 2+ hyphens with a space
$string =~s/-//g; # Remove hyphens
$string =~ s/\s=/ /g;
return($string);
}
sub onLeft {
#USAGE: $word = onLeft($string, $radius, $ordinal);
my $left = substr($_[0], 0, $_[1]);
$left = removePunctuation($left);
my @word = split(/\s+/, $left);
return($word[-$_[2]]);
}
sub byLeftWords {
my $left_a = onLeft($a, $radius, $ordinal);
my $left_b = onLeft($b, $radius, $ordinal);
lc($left_a) cmp lc($left_b);
}
# process each line in the list of lines
print "Content-type: text/plain\n\n";
my $line_number = 0;
foreach my $x (sort byLeftWords @lines){
++$line_number;
printf "%5d",$line_number;
print " $x\n\n";
}
# done
exit;
See alos [Lingua :: Concordance](https://metacpan.org/pod/Lingua:Concordance) –
所以......你已經寫了100多行代碼---你在這裏已經傾倒了整個---但你甚至不能嘗試使用['glob'](http://perldoc.perl.org/functions/glob.html)或['readdir'](http: //perldoc.perl.org/functions/readdir.html)掃描目錄? –
@Matt,你的回答也沒有幫助。 Deep Shah在一個半月前已經有了足夠的麻煩,試圖讓他的CGI工作,並再次陷入困境。這是他大量代碼轉儲的來源。希望你的'readdir'建議可能會取得一些進展......但是@Matt可以做得更好 - grtzzz – vanHoesel