1

特定の文字列で一致させる必要がある部分文字列を含むファイルがあります。これらの特定の文字列は、実際のデータを含む別のファイルから取得されます。これは csv ファイルの列です。指定された文字列にこれらの部分文字列のいずれかが含まれている場合、TRUE としてマークされます。これを行う最善の方法は Perl ですか?

今までやってきたことは、こんな感じです。まだいくつかの問題があるようです:

#!/usr/bin/perl

use warnings;
use strict;

if ($#ARGV+1 != 1) {
 print "usage: $0 inputfilename\n";
 exit;
}

our $inputfile = $ARGV[0];
our $outputfile = "$inputfile" . '.ads';
our $ad_file = "C:/test/easylist.txt";  
our %ads_list_hash = ();

our $lines = 0;

# Create a list of substrings in the easylist.txt file
 open ADS, "$ad_file" or die "can't open $ad_file";
 while(<ADS>) {
        chomp;
        $ads_list_hash{$lines} = $_;
        $lines ++;
 }  

 for(my $count = 0; $count < $lines; $count++) {
            print "$ads_list_hash{$count}\n";
       }
 open IN,"$inputfile" or die "can't open $inputfile";       
 while(<IN>) {      
       chomp;       
       my @hhfile = split /,/;       
       for(my $count = 0; $count < $lines; $count++) {
            print "$hhfile[10]\t$ads_list_hash{$count}\n";

            if($hhfile[9] =~ /$ads_list_hash{$count}/) {
                print "TRUE !\n";
                last;
            }
       }
 }

 close IN;
4

3 に答える 3

2

Text::CSVを参照してください- コンマ区切り値マニピュレータのような

use 5.010;
use Text::CSV;
use Data::Dumper;
my @rows;
my %match;
my @substrings = qw/Hello Stack overflow/;
my $csv = Text::CSV->new ( { binary => 1 } )  # should set binary attribute.
                 or die "Cannot use CSV: ".Text::CSV->error_diag ();
open my $fh, "<:encoding(utf8)", "test.csv" or die "test.csv: $!";
while ( my $row = $csv->getline( $fh ) ) {
        if($row->[0] ~~ @substrings){ # 1st field 
            say "match " ;
            $match{$row->[0]} = 1;
        }
 }
$csv->eof or $csv->error_diag();
close $fh;
print Dumper(\%match);
于 2011-03-14T10:25:40.040 に答える
1

$hhfile[10]テストする前に各広告パターンと一緒に印刷されないことを除いて、投稿したコードと同じことを行うクリーンアップされたコードを次に示します。その出力が必要な場合は、すべてのパターンをループして、基本的に既に行っているのと同じ方法で各パターンを個別にテストする必要があります。(ただし、その場合でも、ループがfor my $count (0 .. $lines)C スタイルの代わりにあった方がよいでしょうfor (...;...;...)。)

各パターンを個別にテストする代わりに、Regexp::Assembleを使用しました。これは、個々の部分文字列すべてを一度にテストするのと同等の単一のパターンを構築します。Nikhil Jain の回答のスマート マッチ演算子 ( ~~) は、彼の回答に示されているように使用すると基本的に同じことを行いますが、Perl 5.10 以降が必要ですが、5.8 または (天国は禁じられています!) 5.6.

#!/usr/bin/env perl

use warnings;
use strict;

use Regexp::Assemble;

die "usage: $0 inputfilename\n" unless @ARGV == 1;

my $inputfile     = $ARGV[0];
my $outputfile    = $inputfile . '.ads';
my $ad_file       = "C:/test/easylist.txt";
my @ad_list;

# Create a list of substrings in the easylist.txt file
open my $ads_fh, '<', $ad_file or die "can't open $ad_file: $!";
while (<$ads_fh>) {
    chomp;
    push @ad_list, $_;
}

for (@ad_list) {
    print "$_\n";       # Or just "print;" - the $_ will be assumed
}      

my $ra = Regexp::Assemble->new;
$ra->add(@ad_list);

open my $in_fh, '<', $inputfile or die "can't open $inputfile: $!";
while (<$in_fh>) {
    my @hhfile = split /,/;
    print "TRUE !\n" if $ra->match($hhfile[9]);
}

( によると、コードは構文的に有効ですが、それperl -c以上はテストされていません。)

于 2011-03-14T14:09:25.123 に答える