将所有数据保存在数组中,过滤掉重复的数据,比较数组之间的数据并删除匹配的数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将所有数据保存在数组中,过滤掉重复的数据,比较数组之间的数据并删除匹配的数据相关的知识,希望对你有一定的参考价值。

我的脚本有一些问题。问题是:

  1. 打印输出时,$ str或@matchedPath的值有时会为空白。它不是随机的,只发生在table.txt文件中的某些Path,我无法弄明白,为什么?
  2. 如何打印结果,因为我找不到table.txt文件的正确文件位置或目录,因为我已将所有路径位置放在一个数组中,过滤它并与table.txt匹配的正确文件位置进行比较因此,打印出来时会丢失一些位置。

/home/is/latest/table.txt文件包含的示例路径,粗体文本是table.txt中的所需路径,

##WHAT PATH IS_THAT,Backup
a   b/c/d   B
a   b/c/d/e  B
a   b/c/d/e/f  B
a   b/c/d/g  B

/home/are/latest/table.txt文件包含的示例路径,中间文本是table.txt中的有用路径,

##WHAT PATH IS_THAT,Backup
a   b/c/d/j B

例如list.txt文件包含,

rty/b
uio/b/c
qwe/b/c/d
asd/b/c/d/e
zxc/b/c/d/e/f
vbn/c/d/e
fgh/j/k/l

预期结果:

Unmatched Path         : b/c/d/g
table.txt file location: /home/is/latest/table.txt

Unmatched Path         : b/c/d/j
table.txt file location: /home/are/latest/table.txt

下面是我的详细脚本,

#!/usr/perl/5.14.1/bin/perl 

# I want to make a script that automatically compare the path in table.txt with list.txt
#table.txt files is located under a parent directory and it differs in the subdirectory.
#There is about 10 table.txt files and each one of it need to compare with list.txt
#The objective is to print out the path that are not in the list.txt

use strict;
use warnings;
use Switch;
use Getopt::Std;
use Getopt::Long;
use Term::ANSIColor qw(:constants);
use File::Find::Rule;
use File::Find;
use File::Copy;
use Cwd;
use Term::ANSIColor;

my $path1='/home';                                                      #Automatically search all table.txt file in this directory even in subdirectory
my $version='latest';                                                   #search the file specified subdirectory e.g. /home/is/latest/table.txt and /home/are/latest/table.txt
my $path2='/list.text';                                                 #there is about 10 table.txt files which contain specified paths in it.

$path1 =~ s/^s+|s+$//g;
$version =~ s/^s+|s+$//g;
$path2 =~ s/^s+|s+$//g;

my @files = File::Find::Rule->file()
                            ->name( 'table.txt' )
                            ->in( "$path1" );

my @symlink_dirs = File::Find::Rule->directory->symlink->in($path1);      #If the directory is a symlink, in my case 'latest' is a symlink directory
print colored (sprintf ("

	SUMMARY REPORT"),'bold','magenta');
print  "

_______________________________________________________________________________________________________________________________________________________

";

if ($version eq "latest")
{
    foreach my $dir (@symlink_dirs) 
    {
        my @filess = File::Find::Rule->file()
                                     ->name( 'table.txt' )
                                     ->in( "$path1" );
        my $symDir=($dir."/"."table.txt");
        $symDir =~ s/^s+|s+$//g;
        my $wantedPath=$symDir;
        my $path_1 = $wantedPath;
        function($path_1);      

    }
}

else 
{

    for my $file (@files)   
    {

        if ($file =~ m/.*$version.*/)
        {
        my $wantedPath=$file;
            my $path_1 = $wantedPath;
            function($path_1);  
        }
    }
}

sub function
    {

    my $path_1 = $_[0];
    open DATA, '<', $path_1 or die "Could not open $path_1: $!";

    my $path_2 = "$path2";
    open DATA1, '<', $path_2 or die "Could not open $path_2: $!";

################# FOCUSED PROBLEM AREA ##############################       
    my @matchedPath;
    my @matched_File_Path;
    my @unmatchedPath;
    my @unmatched_File_Path;
    my @s2 = <DATA1>;

        while(<DATA>)
        {

        my $s1 = $_;

            if ($s1 =~ /^#.*/) 
            {
            next;
            }

            if ($s1 =~ /(.*)s+(.*)s+(.*)s+/) 
            {
            my $str=($2);
            $str =~ s/s+//g;

                for my $s2 (@s2)
                {

                    if ($s2 =~ /.*$str/)
                    {
                    push @matchedPath,$str;                                      
                    push @matched_File_Path,$path_1;                                
                    print "matched Path: $str
	$path_1
"; #I don't understand, sometimes I get empty $str value in this. Can anyone help me?
                    last;
                    }
                    else
                    {
                    #print "unmatch:$str
	$path_1
";                     
                    push @unmatchedPath,$str;                                   
                    @unmatched_File_Path,$path_1;
                                        }
                }
            }
        }
        foreach (@unmatchedPath)
        {print "unmatch path: $_
";}        
        foreach (@matchedPath)
        {print "
match path: $_

";}     

        foreach (@unmatched_File_Path)       
        {print "unmatch File Path: $_
";}
        foreach (@matched_File_Path)         
        {print "match File Path: $_
";}

        my @filteredUnmatchedPath = uniq(@unmatchedPath);                   
        my @filteredUnmatched_IP_File_Path =uniq(@unmatched_IP_File_Path); 
         @filteredUnmatchedPath = grep {my $filteredPath = $_; not grep $_ eq $filteredPath, @matchedPath} @filteredUnmatchedPath;
}
        print "@filteredUnmatchedPath
";   
        print "@filteredUnmatched_IP_File_Path
";

sub uniq 
{
    my %seen;
    grep !$seen{$_}++, @_;
}



close(DATA);
close(DATA1);

print  "_________________________________________________________________________________________________________________________________________________________

";
答案

我认为在这里使用哈希更加简单 这是我尝试过的:你将不得不用包含表存在的每个路径的数组替换@all_path

use strict;
use warnings;

my @all_path =("some/location/table.txt","some/location_2/table.txt");
my %table_paths;
my %list_paths;

foreach my $path (@all_path)
{
    open (my $table, "<", $path) or die ("error opening file");
    #we create hash, each key is a path
    while (<$table>)
    {
        chomp;
        #only process lines starting with "a" as it seems to be the format of this file
        $table_paths{(split)[1]}=$path if (/^a/); #taking the 2nd element in each line
    }

    close $table;
}

open (my $list, "<", "list.txt") or die ("error opening file");

#we create hash, each key is a path
while (<$list>)
{
    chomp;
    $list_paths{$_}=1;
}

close $list;

#now we delete from table_paths common keys with list, that lefts unmathed
foreach my $key (keys %table_paths)
{
    delete $table_paths{$key} if (grep {$_ =~ /$key$/} (keys %list_paths));
}

#printing unmatched keys
print "unmatched :$_
location: $table_paths{$_}

" foreach keys %table_paths;

inputs

在some / location / table.txt中

##WHAT PATH IS_THAT,Backup
a   b/c/d   B
a   b/c/d/e  B
a   b/c/d/e/f  B
a   b/c/d/g  B

在some / location_2 / table.txt中

##WHAT PATH IS_THAT,Backup
a   b/c/d/j B

在list.txt中

rty/b
uio/b/c
qwe/dummyName/b/c/d
asd/b/c/d/e
zxc/b/c/d/e/f
vbn/c/d/e
fgh/j/k/l

output:

unmatched: b/c/d/g
location: some/location/table.txt

unmatched: b/c/d/j
location: some/location_2/table.txt

以上是关于将所有数据保存在数组中,过滤掉重复的数据,比较数组之间的数据并删除匹配的数据的主要内容,如果未能解决你的问题,请参考以下文章

如何取出数组中相同的对象

如何从 ReactJs 中过滤掉数组中的数据

合并两个数组,并且把相同的数值覆盖掉

如何从 jquery 对象映射中过滤唯一的数据属性值

常见的数组去重方法

过滤数组以具有唯一值[重复]