检查哈希键中是否存在模式

Check if pattern exists in a keys of hash

我有以下模式的散列

my %hash_table(
     23                => someintegertype,
     type              => somestringtype,
     12_someidentifier => someveryproblematictype
);

如何检查 12_someidentifier 键遵循的模式是否存在于散列中?如果是这样,我需要知道 truefalse.

形式的值

::更新:: 我想检查 {[\d]_[\w+]} 等正则表达式或模式是否存在?

exists 告诉您密钥是否存在。 $hash{$key} 给你的值,所以你可以测试它。

如果您希望根据正则表达式测试多个值(例如散列的键),那么该工作的工具是 grep;

my @matches = grep { /\d+_\w+/ } keys %hash_table;
print @matches;

就在这时 - 打开 use strict;use warnings;。这将有助于长期 运行。

您可以这样检查:

if (exists $hash_table{_someidentifier})
{
        print _someidentifier, "\n";
}