在Perl中输入多个.txt文件
问题描述:
我有一个在线的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;
答
的3210函数将返回匹配的模式,其文件的列表。
my @text_files = glob('*.txt');
当然,您可能不需要中间变量@text_files
变量。
while (my $file = glob('*.txt')) {
open my $fh, '<', $file or die "$file: $!";
# do something with the filehandle
}
其他有关您的代码的建议。
-
-w
在很大程度上与use warnings
更换时的Perl 5.6在2000年 -
new CGI
被释放远不如写成CGI->new
。 - 对特殊变量(如
$/
)的更改应始终进行本地化。 - 请使用词法文件句柄和
open()
的三个参数版本(如我上面的示例中所演示的)。 - 如果您使用CGI.pm,那么为什么不使用它的
header()
方法呢?
但是,最重要的是,请重新考虑您对CGI的使用。请更好地阅读CGI::Alternatives(我的意思是更简单和更强大)的建议。
+0
非常感谢您的帮助 –
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