Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.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中的Instr等价物?_Regex_Perl_Hash - Fatal编程技术网

Regex perl中的Instr等价物?

Regex perl中的Instr等价物?,regex,perl,hash,Regex,Perl,Hash,perl中的新任务(我以前从未使用过)。所以请帮助我,尽管这听起来很愚蠢 名为RestrictedNames的变量保存受限制的用户名列表。SplitNames是一个数组变量,它保存完整的用户名集。现在我必须检查是否在RestrictedNames变量中找到了当前名称,就像使用instr一样 @SplitNames=(“纳格·阿尔加茨”,“阿文德·辛格”,“阿比·阿瓦斯蒂”,“卢夫·辛格”,“新阿尔加茨”),现在我想屏蔽所有带有“辛格”,“阿尔加茨”等的姓氏 @SplitNames=(“纳格藻类”

perl中的新任务(我以前从未使用过)。所以请帮助我,尽管这听起来很愚蠢

名为RestrictedNames的变量保存受限制的用户名列表。SplitNames是一个数组变量,它保存完整的用户名集。现在我必须检查是否在RestrictedNames变量中找到了当前名称,就像使用instr一样

@SplitNames=(“纳格·阿尔加茨”,“阿文德·辛格”,“阿比·阿瓦斯蒂”,“卢夫·辛格”,“新阿尔加茨”),现在我想屏蔽所有带有“辛格”,“阿尔加茨”等的姓氏

@SplitNames=(“纳格藻类”、“阿文德·辛格”、“阿瓦斯蒂”、“卢夫·辛格”、“新藻类”)
$RestrictedNames=“tiwary singh algates n2 n3 n4 n5 n6”;

对于(my$i=0;$i您应该修改此行:

if($RestrictedNames =~ m/^$SplitNames[$i]/ )

^
从头开始查找匹配项

有关perl元字符的更多详细信息,请参阅

编辑: 如果需要基于姓氏进行阻止,请在for循环体中尝试此代码

my @tokens = split(' ', $SplitNames[$i]); # splits name on basis of spaces
my $surname = $tokens[$#tokens]; # takes the last token
if($RestrictedNames =~ m/$surname/ )
{
      print "$SplitNames[$i] is a restricted person\n";
}

不要尝试处理一组受限名称,而是处理一个数组

然后只需使用(
~
或两个波浪号字符)查看其中是否有给定的字符串

#!/usr/bin/perl
use v5.12;
use strict;
use warnings;

my $RestrictedNames="n1 n2 n3 n4 n5 n6 n7 n8 n9";
my @restricted_names = split " ", $RestrictedNames;
say "You can't have foo" if 'foo' ~~ @restricted_names;
say "You can't have bar" if 'bar' ~~ @restricted_names;
say "You can't have n1" if 'n1' ~~ @restricted_names;
say "You can't have n1a" if 'n1a' ~~ @restricted_names;

使用哈希切片尝试以下操作:

my @users =  ( "n10", "n12", "n13", "n4", "n5" );
my @r_users = ( "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9" ) ;
my %check;
@check{@r_users}  = ();
foreach my $user ( @users ) {
   if ( exists $check{$user} ) {
      print"Restricted User: $user  \n";
   }
}

最惯用的方法是创建一个受限名称的散列,然后将姓氏从名称中分离出来,并检查该姓氏是否在散列中

use strict;
use warnings;

my @SplitNames = ("naag algates","arvind singh","abhay avasti","luv singh","new algates");
my $RestrictedNames = "tiwar y singh algates n2 n3 n4 n5 n6";

# Create hash of restricted names
my %restricted;
map { $restricted{$_}++ } split ' ', $RestrictedNames;

# Loop over names and check if surname is in the hash
for my $name (@SplitNames) {
    my $surname = (split ' ', $name)[-1];
    if ( $restricted{$surname} ) {
        print "$name is a restricted person\n";
    }
}
请注意,
split
函数通常使用正则表达式。但是,将
'
与split一起使用是一种特殊情况。它可以拆分任意长度的空格,也可以忽略任何前导空格,因此对于拆分单个单词的字符串非常有用


仅供参考,perl中的
instr
等效于使用
索引($string,$substring)
。如果
$substring
没有出现在
$string
中,它将返回
-1
。任何其他值都意味着
$string
包含
$substring
。但是,在比较列表时,使用如上所示的散列就不那么麻烦了……而且与索引不同,当您真正想要的是为了匹配“joy”。

您期望的结果是什么?下面是我使用的示例
$RestrictedNames=“n1 n2”;@SplitNames=('n1','n2','n3');
输出:n1,n2yes我忘了提供@SplitNames!@SplitNames=('naag algates”,“arvind singh”,“abhay avasti”,“luv singh”)的数据,现在我想阻止所有带有“singh”的姓氏实际上,我想要一个结果,其中“arvind singh”和“luv singh”在限制名称下。BASIC的
instr
work alikes在Perl中是和。
my @users =  ( "n10", "n12", "n13", "n4", "n5" );
my @r_users = ( "n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9" ) ;
my %check;
@check{@r_users}  = ();
foreach my $user ( @users ) {
   if ( exists $check{$user} ) {
      print"Restricted User: $user  \n";
   }
}
use strict;
use warnings;

my @SplitNames = ("naag algates","arvind singh","abhay avasti","luv singh","new algates");
my $RestrictedNames = "tiwar y singh algates n2 n3 n4 n5 n6";

# Create hash of restricted names
my %restricted;
map { $restricted{$_}++ } split ' ', $RestrictedNames;

# Loop over names and check if surname is in the hash
for my $name (@SplitNames) {
    my $surname = (split ' ', $name)[-1];
    if ( $restricted{$surname} ) {
        print "$name is a restricted person\n";
    }
}