使用 Perl6 散列键定义与存在 [英] Defined vs. exists with Perl6 hash keys

查看:33
本文介绍了使用 Perl6 散列键定义与存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 Perl5 学习 Perl6.

I am learning Perl6 from Perl5.

我在看副词 :exists https://docs.perl6.org/type/Hash#:exists 但没有 :defined 副词

I am looking at the adverb :exists https://docs.perl6.org/type/Hash#:exists but there isn't a :defined adverb

但我很担心,因为 perl5 的 exists &defined:存在和定义有什么区别?

but I am concerned because there is a distinction between perl5's exists & defined: What's the difference between exists and defined?

我怎样才能在 Perl6 中做这样的事情?

How can I do something like this in Perl6?

if (defined $hash{key}) {
   $hash{key}++;
} else {
   $hash{key} = 1;
}

推荐答案

if defined %hash{'key'} {
   %hash{'key'}++;
} else {
   %hash{'key'} = 1;
}

使用 defined 例程或方法.请参阅 5to6-perlfunc -- 已定义

Use the defined routine or method. See 5to6-perlfunc -- defined

这篇关于使用 Perl6 散列键定义与存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆