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 带子字符串的密钥存在性测试_Regex_Perl_Hash - Fatal编程技术网

Regex 带子字符串的密钥存在性测试

Regex 带子字符串的密钥存在性测试,regex,perl,hash,Regex,Perl,Hash,我有一个多维散列/数组,它使用datadumper看起来像这样 { 'Customer' => {'123abc' => {'status' => {'New' => {'email'=>['user@xxx.com' ],

我有一个多维散列/数组,它使用datadumper看起来像这样

{
          'Customer' => {'123abc' => 
                         {'status' => 
                                     {'New' => 
                                              {'email'=>['user@xxx.com' ], 
                                               'template' => 'XYZ' }
                                                                       }
                                                           },
                        '234' => 
                        {'status' => 
                                    {'New' => 
                                            {'email' => ['user@xxx.com' ],
                                            'template' => 'XYZ' }
                                                                            }
                                                                }

$customers = ("123abc", "234abc", "adb234");
我需要根据数组值的完全或部分匹配来测试是否存在客户

我用于完全匹配的代码运行良好

foreach (@customers) {
if ($config->{Customer}->{$customers[0]}) {
do something
} }
这将返回“123abc”上的匹配项

但是,如果$customers[0]中有字符串234,或者在没有数组$customers的情况下仅使用字符串进行测试,则无法使其匹配

我试过了

if (/.234*$/ ~~ %config->{Customer})
基于此网站“打印”上的智能匹配示例,我们有一些青少年\n“if/*teen$/~~%h;”

以及在正则表达式的开头使用m。{m/234/}

乔恩


这是用perl编写的。

看起来您希望对所有键进行grep

my @keys = grep { /234/ } keys %{$config->{Customer}};
if (@keys) {
  # do something, but check for multiple matches...
}
Grep返回块计算结果为true的所有元素,每个元素用$表示。正则表达式匹配(//)默认为与$匹配。上述声明可以改写为

my @keys = grep { $_ =~ /234/ } keys %{$config->{Customer}};
if (@keys) {
  # do something, but check for multiple matches...
}

但只要您熟悉perl,这是多余的。

234 in$customers[0]应该与什么匹配?“234abc”、“adb234”这两个词??如果你用传统的
“value”=~/regex/
语法替换智能匹配
/regex/~~“value”
语法会怎么样?另外,regex
/.234*$/
看起来有点过时;您可能指的是
/.*234$/
,它更优雅地写为
/234$/
。引用
$config->{Customer}
引用的哈希的正确方法是
%{$config->{Customer}
。您当然不能使用正则表达式作为散列键;它将被简单地解释为一个文本字符串
$config->{m/234/'}
。如果你
使用strict,很多事情都会变得清晰;使用警告my @keys = grep { $_ =~ /234/ } keys %{$config->{Customer}};
if (@keys) {
  # do something, but check for multiple matches...
}