简单的Perl字符串与特殊字符匹配,即'

时间:2013-07-07 02:34:41

标签: perl

my $var_cot_descn="Veteran's Affairs";
my %cot_descn= ("Correctional","Veteran's Affairs","State Pharmacy Assistance Program","VA Medical Center","VA Mail Order Pharmacy","PHS 340B Entity");

if (exists $cot_descn{$var_cot_descn}){
    print  "CustomerDomain=\"GOV\"
        />
        </Org>
        \n";
}
else{
    print  "CustomerDomain=\"COM\"
        />
        </Org>
        \n";
}

所有工作都很好,除非我与“退伍军人事务”进行比较,导致if条件失败。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:5)

您的哈希没有"Veteran's Affairs"密钥,它具有"Veteran's Affairs" 。如果您以更常见的方式定义哈希:

my %cot_descn = (
    "Correctional" => "Veteran's Affairs",
    "State Pharmacy Assistance Program" => "VA Medical Center",
    "VA Mail Order Pharmacy" => "PHS 340B Entity"
);
然后结构会更清晰。由于没有"Veteran's Affairs"密钥,因此该测试会将您发送到else分支:

if (exists $cot_descn{$var_cot_descn})

$var_cot_descn"Veteran's Affairs"时。

也许你的%cot_descn向后有第一个键/值。

答案 1 :(得分:0)

你的意思是:

#!/usr/bin/env perl

use strict;
use warnings;

# --------------------------------------
use Data::Dumper;

# Make Data::Dumper pretty
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Indent   = 1;

# Set maximum depth for Data::Dumper, zero means unlimited
local $Data::Dumper::Maxdepth = 0;

# --------------------------------------

my @cot_descn = (
        "Correctional",
        "Veteran's Affairs",
        "State Pharmacy Assistance Program",
        "VA Medical Center",
        "VA Mail Order Pharmacy",
        "PHS 340B Entity",
    );
my %cot_descn = map { $_ => 1 } @cot_descn;

print Dumper \%cot_descn;