简体   繁体   English

使用两个元素在perl中创建哈希

[英]Create hash in perl with two elements

How do you write the following in Perl(provide exact Perl syntax): a) create hash "days_in_month" (hash variable name is "days_in_month") with two elements. 如何在Perl中编写以下内容(提供精确的Perl语法):a)使用两个元素创建哈希“days_in_month”(哈希变量名称为“days_in_month”)。 First element with key "July" and value 31, second element with key "September" and value 30. Get hash element by key "September" and print its value (provide exact Perl syntax). 第一个元素,键为“July”,值为31,第二个元素为键“九月”,值为30.按键“九月”获取哈希元素并打印其值(提供精确的Perl语法)。 What value should you get? 你应该得到什么价值?

Would it be like this ? 会这样吗?

       my %days_in_month = (
          'July' => '31',
          'September' => '30',
           );

       my $dayvalue = $days_in_month{September};
       print $dayvalue; 

Yes, but you don't need to quote the key, and you really shouldn't quote numbers used as numbers. 是的,但你不需要引用密钥,你真的不应该引用用作数字的数字。

my %days_in_month = (
          July => 31,
          September => 30,
           );

This is equivalent to the following code: 相当于以下代码:

my %days_in_month = ('July' => 31,'September' => 30);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM