2017-02-26 91 views
-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; 
+0

See alos [Lingua :: Concordance](https://metacpan.org/pod/Lingua:Concordance) –

+1

所以......你已經寫了100多行代碼---你在這裏已經傾倒了整個---但你甚至不能嘗試使用['glob'](http://perldoc.perl.org/functions/glob.html)或['readdir'](http: //perldoc.perl.org/functions/readdir.html)掃描目錄? –

+0

@Matt,你的回答也沒有幫助。 Deep Shah在一個半月前已經有了足夠的麻煩,試圖讓他的CGI工作,並再次陷入困境。這是他大量代碼轉儲的來源。希望你的'readdir'建議可能會取得一些進展......但是@Matt可以做得更好 - grtzzz – vanHoesel

回答

1

的​​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

非常感謝您的幫助 –