我正在尋找的txt文件,找到包含一些字符的文本,而在另一個文件夾移動他們...PERL - 搜索文本字符串,得到的結果與子以及
我正在尋找的以下兩個關鍵字:
95-B/A
95-ASB/A
我的代碼看起來像
月1日編輯:把整個代碼
use warnings;
use File::Copy;
use File::Basename;
my (%count,%countNegative,%countPositive,$i,$j,$key,@keys,@keysNegative,@keysPositive,$token,$tokenNegative,$tokenPositive,@tokens,@tokensNegative,@tokensPositive,$totalCount,$negativeCount,$positiveCount,$totalCountNegativeInText,$totalCountPositiveInText);
@files = <*.txt>;
foreach $fileToProcess (@files) {
open(INFILE,"<$fileToProcess") or die("cannot open file");
while (<INFILE>) {
@tokens = &tokenize($_);
foreach $token (@tokens) {
if ($token =~ /[a-zA-Z]/) {
$count{$token} = $count{$token} ? $count{$token}+1 : 1;
}
}
}
@keys = keys %count;
@keys = sort { $count{$b} <=> $count{$a} } @keys;
for ($i=0;$i<=$#keys;$i++) {
if ((lc $keys[$i] eq lc '95-B/A') || (lc $keys[$i] eq lc '95-ASB/A')) {
$oldlocation = $fileToProcess;
$newlocation = '95BA';
File::Copy::move($oldlocation, $newlocation);
}
}
close(INFILE);
}
exit(0);
use strict;
my $true = 1;
my $false = 0;
my $text = "";
my $word;
# read text
while (<>) { $text .= $_; }
foreach $word (&tokenize($text)) {
&printText(&rule3(&rule2(&rule1(&makeUnits(&cleanUp($word))))));
}
print "\n";
exit(0);
sub tokenize {
$_ = $_[0];
s/\s+/\n/g;
s/^\n//;
s/$/\n/;
s/([.,!?:;,])\n/\n$1\n/g;
s/\n(["'`])([^\n])/\n$1\n$2/g;
s/([^\n])(["'`])\n/$1\n$2\n/g;
s/([^\n])([.,])\n/$1\n$2\n/g;
s/\n([A-Z])\n\./\n$1./g;
s/\n\.\n([^"A-Z])/\.\n$1/g;
s/(\.[A-Z]+)\n\.\n/$1.\n/g;
s/([^\n])'s\n/$1\n's\n/g;
s/([^\n])n't\n/$1\nn't\n/g;
s/([^\n])'re\n/$1\n're\n/g;
s/\n\$([^\n])/\n\$\n$1/g;
s/([^\n])%\n/$1\n%\n/g;
s/Mr\n\.\n/Mr.\n/g;
return(split(/\n/,$_));
}
sub printText {
my $i;
for ($i=0;$i<@_;$i++) {
print join('',reverse(split(//,&breakUnits($_[$i]))));
}
print " ";
}
它選擇那些有95-B/A 95-ASB/A的人,但它也選擇那些有95-B和95-ASB的人(我不想那樣,我只想挑選那些有95-B/A和95-ASB/A)。
我想我在處理正斜槓時出錯了嗎?有沒有人有辦法解決嗎?
在此先感謝
第二編輯:我想如果我把循環在那裏我找到令牌裏面的字符串檢查,它工作得很好。顯然我被搞亂鑰匙放在了哈希,當我在做請檢查下一步,但我沒有看到我不應該在第一步使用令牌的原因。您是怎麼想的?
@files = <*.txt>;
foreach $fileToProcess (@files) {
open(INFILE,"<$fileToProcess") or die("cannot open file");
while (<INFILE>) {
@tokens = &tokenize($_);
foreach $token (@tokens) {
if ($token =~ /[a-zA-Z]/) {
if (($token eq '95-B/A') || ($token eq '95-ASB/A'))
{
$oldlocation = $fileToProcess;
$newlocation = '95BA';
File::Copy::move($oldlocation, $newlocation);
}
$count{$token} = $count{$token} ? $count{$token}+1 : 1;
}
}
}
https://eval.in/216716 – 2014-11-03 16:26:33
Perl。這是Perl。 – Borodin 2014-11-03 16:51:37
您需要向您展示*真實*代碼。我確定這不是它,因爲'$ oldlocation'和'$ newlocation'在循環內部不會改變,並且獨立於散列鍵。如果這是真的,那麼每次碰巧在任意哈希中找到匹配時,都會移動相同的文件。 – Borodin 2014-11-03 16:59:40