小编daf*_*dil的帖子

比较两个数组并找出差异

我需要比较两个数组并得到差异。

背景:

第一个阵列将列出文件夹中的文件。

第二个数组将读取文件的内容并存储在数组中。

第一个数组的输出将是

a
b
c
d
e
Run Code Online (Sandbox Code Playgroud)

第二个数组的输出将是

a
b
c
e
Run Code Online (Sandbox Code Playgroud)

我怎样才能比较得到差异的那两个数组?我想要的结局输出是

d
Run Code Online (Sandbox Code Playgroud)

这是代码:

#!/usr/bin/perl

use strict;
use warnings;

my $list  = "experiment.sv";
my $path  = "../../../folder1/";
my $filelist;

open ( OUTFILE, ">output.txt" );
main ();
close OUTFILE;


sub main {

   my @array1;
   opendir ( DIR, $path ) || die "Error in opening dir $path\n"; 
   while ( $filelist = readdir (DIR) ) {
       next if ( $filelist =~ s/\.//g);   #/
       push @array1, $filelist;         
   }
   closedir(DIR);

   my …
Run Code Online (Sandbox Code Playgroud)

arrays algorithm perl comparison

4
推荐指数
1
解决办法
81
查看次数

查找不包含特定字符串的文件

我要查找不包含特定字符串的文件吗?列出的文件如下所示

../../../experiment/fileA.txt (contain word 'Book')
../../../experiment/fileB.txt (contain word 'Book')
../../../experiment/fileC.txt (do not contain word 'Book')
../../../experiment/fileD.txt (contain word 'Book')
Run Code Online (Sandbox Code Playgroud)

这是我的代码

use strict;
use warning;

my $dirname = "../../../experiment/";
my $keyword = "Book";
my @result;

my $find_file = sub {
    my $F = $File::Find::name;
    if ($F =~ /txt$/) {
       open my $in, "<", $F or die $!;
       while(<$in>) {
          if (/\Q$keyword\E/){
             next;
          }else{
             push @result, $F;
             return;          
          }
      }
   }  
};

find ({ wanted => $find_file, no_chdir=>1}, $dirname );
foreach my …
Run Code Online (Sandbox Code Playgroud)

perl

1
推荐指数
1
解决办法
45
查看次数

标签 统计

perl ×2

algorithm ×1

arrays ×1

comparison ×1