Instr Equivalent in perl?

new task in perl (which I have never used before). So please help me though it sounds silly.

A variable named RestrictedNames contains a list of restricted user names. SplitNames is an array variable that contains the full set of username. Now I need to check if the current name is found in the RestrictedNames variable, for example using instr.

@SplitNames = ("naag algates", "arvind singh", "abhay avasti", "luv singh", "new algates"), and now I want to block all surnames that have "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";
    }
}

I ask you to help me in solving the problem. If this is already set, forgive me and share this link.

+3
4

:

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

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

^ .

perl .

: , .

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

, .

smart match (~~ ), , .

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

- Hash Slice:

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

- , , .

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 RegEx. ' ' split - . , , .

FYI, instr perl - index($string, $substring). $substring $string, -1. $string $substring. , , ... , "", "".

0

All Articles