English 中文(简体)
皮尔岛的等值?
原标题:Instr Equivalent in perl?
  • 时间:2012-05-25 07:28:00
  •  标签:
  • perl

名为 restrictedNames 的变量持有限制用户名列表。 SplitNames 是一个保留完整用户名集的数组变量。 现在我必须检查是否在 restrictedNames 变量中找到当前名称, 如使用 instr

@SplitNames = ("naag algates","arvind singh","abhay avasti","luv singh","new algates") and now i want to block all the surnames which has "singh" ,"algates" etc.

    @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";
        }
    }
最佳回答

您应该修改此行 :

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

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

从一开始寻找匹配 。

关于 Perl 元字符的更多详情,请见这里

EDIT: If you need blocking based on surnames, try this code in the for-loop body.

my @至kens = split(   , $SplitNames[$i]); # splits name on basis of spaces
my $surname = $至kens[$#至kens]; # takes the last 至ken
if($RestrictedNames =~ m/$surname/ )
{
      print "$SplitNames[$i] is a restricted person
";
}
问题回答

不要试图处理一连串限制名称, 处理一个阵列。

然后使用""http://perldoc.perl.org/perlsyn.html#Smart-matching-in-detail" rel=“不跟随 Noreferrer" >智能匹配接线员 ( 或两个tilde字符) 来查看其中是否有给定字符串 。

#!/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;

尝试在下面使用Hash Slicle:

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  
";
   }
}

最普通的方式是制造一个限制名称的散列,然后将姓氏与姓名分开,并检查姓氏是否在散列中。

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
";
    }
}

请注意, split 函数通常需要一个 RegEx。 但是使用 和拆分是一个特例。 它在任何长的白色空格上分割, 并且忽略任何领先的白空格, 因此它对于分隔单个单词的字符串有用 。

FYI, 在 perl 中, 相当于 < code> insstr < / code > 的值是使用 < code> index( $string, $substring) 。 如果 < code > $substring < / code > 不出现在 < code > $string < / code > 内部, 它会返回 < code> - 1 < / code > 。 任何其他值表示 < code > $sstring < / code > 包含 < code > asubstring 。 但是, 在比较列表时, 像我以上显示的那样使用散列时, 它会少得多的仓促使用. 与索引不同, 它不会匹配喜乐, 当你真正想要匹配喜乐时 。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...