Instr等价于perl?

Nag*_*ram 3 regex perl hash

perl中的一项新任务(我之前从未使用过).所以请帮助我,即使这听起来很傻.

名为RestrictedNames的变量包含受限用户名列表.SplitNames是一个数组变量,它包含完整的用户名集.现在我必须检查RestrictedNames变量中是否找到当前名称,如使用instr.

@SplitNames =("naag algates","arvind singh","abhay avasti","luv singh","new algates")现在我想要阻止所有有"singh","algates"等的姓氏.

@SplitNames = ("naag algates","arvind singh","abhay avasti","luv singh","new algates")
$RestrictedNames="tiwary singh algates n2 n3 n4 n5 n6";
for(my $i=0;$i<@SplitNames;$i++)
{
    if($RestrictedNames =~ m/^$SplitNames[$i]/ ) //google'd this condition, still fails
    {
          print "$SplitNames[$i] is a restricted person";
    }
}
Run Code Online (Sandbox Code Playgroud)

我恳请你帮我解决问题.如果已经提出这个问题,请原谅我并分享该链接.

sud*_*03r 6

你应该修改这一行:

if($RestrictedNames =~ m/^$SplitNames[$i]/ )
Run Code Online (Sandbox Code Playgroud)

if($RestrictedNames =~ m/$SplitNames[$i]/ )
Run Code Online (Sandbox Code Playgroud)

^ 从头开始寻找比赛.

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

编辑: 如果您需要基于姓氏的阻止,请在for-loop正文中尝试此代码.

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";
}
Run Code Online (Sandbox Code Playgroud)


Que*_*tin 5

不要尝试处理一串限制名称,处理数组.

然后只需使用智能匹配运算符(~~或两个波形符)来查看给定的字符串是否在其中.

#!/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;
Run Code Online (Sandbox Code Playgroud)