Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 删除perl中包含重复正则表达式的行_Regex_Perl_Duplicates_Unique - Fatal编程技术网

Regex 删除perl中包含重复正则表达式的行

Regex 删除perl中包含重复正则表达式的行,regex,perl,duplicates,unique,Regex,Perl,Duplicates,Unique,我有一个数组,其中包含如下元素: @array = qw/ john jim rocky hosanna/; 输入文件: 所需输出: 所以,我只需要第一次出现的行。制作另一个数组来指示名称是否已被使用,怎么样?然后,第一次使用Jim读取行时,将该数组中的变量设置为已用变量,并写入输出。如果过去已经使用过,请不要执行任何操作 @array =(john,jim,rocky,hosanna); @used =(0,0,0,0); 制作另一个数组来指示该名称是否已被使用,怎么样?然后,第一次使用J

我有一个数组,其中包含如下元素:

@array = qw/ john jim rocky hosanna/;
输入文件: 所需输出:
所以,我只需要第一次出现的行。

制作另一个数组来指示名称是否已被使用,怎么样?然后,第一次使用Jim读取行时,将该数组中的变量设置为已用变量,并写入输出。如果过去已经使用过,请不要执行任何操作

@array =(john,jim,rocky,hosanna);
@used =(0,0,0,0);

制作另一个数组来指示该名称是否已被使用,怎么样?然后,第一次使用Jim读取行时,将该数组中的变量设置为已用变量,并写入输出。如果过去已经使用过,请不要执行任何操作

@array =(john,jim,rocky,hosanna);
@used =(0,0,0,0);

单程。我将数组数据保存到散列中,并在输入文件中找到条目时删除该条目

script.pl的内容:

输出:


单程。我将数组数据保存到散列中,并在输入文件中找到条目时删除该条目

script.pl的内容:

输出:

希望这能奏效+


希望这能奏效+

您尝试过什么?什么不起作用?StackOverflow不是一个为我做作业的网站。你尝试了什么?什么不起作用?StackOverflow不是一个为我做家庭作业的网站。非常感谢,非常棒!!诸如不带引号的字符串john之类的警告可能与script.pl第3行的将来保留字冲突。在以后处理大型计算机时,此警告是否会生效files@user1228191这来自你自己的台词:@array=john,jim,rocky,hosanna,你忘了在parens之前添加qw。谢谢!!还有一件事,如果在文件中间找到数组元素。乔治和约翰去了欧洲。。。它不会删除我概括了我的问题,请检查@user1228191:更新了我的答案。非常感谢,效果很棒!!诸如不带引号的字符串john之类的警告可能与script.pl第3行的将来保留字冲突。在以后处理大型计算机时,此警告是否会生效files@user1228191这来自你自己的台词:@array=john,jim,rocky,hosanna,你忘了在parens之前添加qw。谢谢!!还有一件事,如果在文件中间找到数组元素。乔治和约翰去了欧洲。。。它不会删除我概括了我的问题,请检查@USER 1228 191:更新了我的答案。还有一件事,如果数组元素被发现在文件的中间。乔治和约翰去了欧洲。。。它不会删除该行——@ USER 1228 191——使用分裂结果的内部GRIP,或者使用\b字边界锚定的正则表达式。乔治和约翰去了欧洲。。。它不会删除该行-@user1228191-使用拆分结果的内部grep,或使用\b单词边界锚定的正则表达式。
@array =(john,jim,rocky,hosanna);
@used =(0,0,0,0);
use warnings;
use strict;

## Input names to search.
my @array = qw/ john jim rocky hosanna/;

## Save names to a hash. This way they are easier to find out.
my %names = map { $_ => 1 } @array;

## Read file line by line.
while ( <> ) { 

    ## Avoid blank lines.
    next if m/\A\s*\Z/;

    ## Split line in fields.
    my @f = split;

    ## Count number of names in hash.
    my $num_entries = scalar keys %names;

    ## Remove words of hash found in line.
    for ( @f ) { 
        delete $names{ $_ };
    }   

    ## If now there are less names, it means that line had any of
    ## them, so print line.
    if ( scalar keys %names < $num_entries ) { 
        printf qq[%s\n], $_; 
    }   

    ## If hash is empty, there are no lines left to print, so exit of
    ## loop without checking more lines.
    last if scalar keys %names == 0;
}
perl script.pl infile
john wears blue shirt 

hosanna knows drawing

george and jim went to europe

rocky went to swimming
@seen{@array} = ();
@out = grep { (($w)=split; !($seen{$w}++) } @in;
perl -ane 'print unless $a{$F[0]}++ ' inputfile