问题描述
好的,这是一个N00b问题,但它让我很难过:
我有以下Perl代码:
%project_keys =(
cd =>continuous_delivery,
cm =>customer_management,
dem =>需求,
dis =>dis,
do =>devops,
sel =>selection,
seo =>seo
);
printproj_code is $ proj_code\\\
; ($ key $ value = $ project_keys){
if($ key == $ proj_code){
$ url =http:// projects /。$ project_keys {$}键 /二传手。
最后;
$ proj_code
总是以相同的('dis')
传递,打印行显示。然而,每次运行这个时,我都会得到 project_keys {$ key}
的不同值。
什么(毫无疑问,显而易见)我做错了什么?我已经看到有关每个脆弱的评论 - 是这样吗?
如果你已经有了钥匙 '期待,检查它是否存在并使用它...另外,总是使用 use strict;使用警告;
#!/ usr / bin / perl
严格使用;
使用警告;
my $ proj_code ='dis';
my $ url;
$ b $ my%project_keys =(
cd =>continuous_delivery,
cm =>customer_management,
dem =>demand,
dis =>dis,
do =>devops,
sel =>selection,
seo =>seo
);
$ url =http:// projects / $ project_keys {$ proj_code} / setter
if $ exists $ project_keys {$ proj_code};
打印url:$ url \\\
; b
$ b 输出: code> url:http:// projects / dis / setter
OK, this is a N00b question, but it's gotten me stumped:
I have the following Perl code:
%project_keys = (
cd => "continuous_delivery",
cm => "customer_management",
dem => "demand",
dis => "dis",
do => "devops",
sel => "selection",
seo => "seo"
);
print "proj_code is $proj_code\n";
while ( ($key, $value) = each %project_keys ) {
if ($key == $proj_code) {
$url = "http://projects/".$project_keys{$key}."/setter";
last;
}
}
$proj_code
is always passed in the same ('dis')
and the print line shows that.
However, each time I run this, I'm getting a different value for project_keys{$key}
.
What (no doubt obvious) thing am I doing wrong? I have seen comments about how each is 'fragile' - is that it?
If you already have the "key" you're expecting, check if it exists and use it...
Also, always use use strict; use warnings;
#!/usr/bin/perl
use strict;
use warnings;
my $proj_code= 'dis';
my $url;
my %project_keys = (
cd => "continuous_delivery",
cm => "customer_management",
dem => "demand",
dis => "dis",
do => "devops",
sel => "selection",
seo => "seo"
);
$url = "http://projects/$project_keys{$proj_code}/setter"
if exists $project_keys{$proj_code};
print "url: $url\n";
OUTPUTS:
url: http://projects/dis/setter
这篇关于我需要重置一个Perl哈希索引吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!