on Jun 20, 2019 at 11:39 UTC ( #11101620=perlquestion: print w/replies, xml ) Need Help??

jimyokl has asked for the wisdom of the Perl Monks concerning the following question:

Reputation: 5

Edit

I just started learning Perl for several days.I sow some codes below, what is the type of $data, is it array or a hash? There are so many nests. Thanks.

my $data = { 'a10' => { b => 1, a => 2, }, 'bbb' => { x => 3, }, 'a2' => { z => 4, } };
Replies are listed 'Best First'.
Re: what is the variable type of $data = {
by Anonymous Monk on Jun 20, 2019 at 12:06 UTC

In this piece of code, {
starts a new, anonymous hash (that doesn't have a variable name of its
own) and returns a reference to it. The reference is stored in the
scalar variable named $data. The hash contains three keys a10, bbb, a2 and the values they have (still scalars) are also hash references.

There are two syntactically different ways to access a hash by reference: $data->{$key} (the arrow operator, ->) and ${$data}{$key} (wrap the reference in {} - where you could also put the name of the hash if it had a name). Both ways take $data, try to dereference it, access the underlying hash and return the scalar value referenced by key $key. If $data does not contain a hash reference, an exception will be raised (but if $data is undef, an anonymous hash may be created automatically and a reference to it may be stored inside $data - see autovivification).

See perlreftut and perldsc for more info.

[reply]
[d/l]
[select]
Re^2: what is the variable type of $data = {
by you!!! on Jun 20, 2019 at 12:14 UTC
Reputation: 1
      Thank you quite a lot for your answer.
[reply]
[edit]
[/msg]

Re: what is the variable type of $data = {

by Eily on Jun 20, 2019 at 12:18 UTC

++ To AnonyMonk, $data is a reference to a hash, that itself contains hashrefs (eg the value at a10 is a hashref). One way to test for that is to use ref, that will return 'HASH' in this case, or Scalar::Util's reftype, that is guarranted to always return 'HASH' even if the reference is blessed (turned into an object).

So you can check that ref $data eq 'HASH'

[reply]
[/msg]
[d/l]
Re^2: what is the variable type of $data = {
by you!!! on Jun 20, 2019 at 12:21 UTC
Reputation: 0
      Thanks for both your quick and detailed answers.
[reply]
[edit]
[/msg]

Re: what is the variable type of $data = {

by LanX on Jun 20, 2019 at 12:20 UTC

      It's a hash of hashes.(HoH)

You might be confused because hashes and arrays have to incarnations in Perl

      • a reference which can be stored in a $scalar
      • a "list form" ° which can be stored in variables with
        appropriate sigils like %hash or @array. (They are effectivity
        containers for references but operating on lists)

      Hashes consist of key => value
      pairs of scalars ( actually

"string" => scalar

      pairs to be precise)

To realize nested hashes you need to put a hash reference into the value slot.

That's what's happening here.

See perldsc for more examples.

HTH :)

Cheers Rolf
(addicted to the Perl Programming Language :)

Wikisyntax for the Monastery
FootballPerl is like chess, only without the dice

°) my wording

what is the variable type of $data = {

by you!!!

Perl monks 的 快速回复的更多相关文章

  1. iOS8推送消息的快速回复处理

    http://blog.csdn.net/yujianxiang666/article/details/35260135 iOS8拥有了全新的通知中心,有全新的通知机制.当屏幕顶部收到推送时只需要往下 ...

  2. 【转载】TCP慢启动、拥塞避免、快速重传、快速回复

    转载自:TCP慢启动.拥塞避免.快速重传.快速回复 转自:http://blog.csdn.net/itmacar/article/details/12278769 感谢博主的辛勤成果! 为了防止网络 ...

  3. discuz X2.0教程]教你快速了解Discuz!程序文件功能,修改文件从此不用再求人

    x3.x数据字典 http://faq.comsenz.com/library/database/x3/x3_index.htm 先从根目录开始,根目录文件一般都是入口,即执行具体功能的代码一般不在这 ...

  4. 1.Perl基础系列之WHAT、WHY、HOW

    What? Perl,一种功能丰富的计算机程序语言,运行在超过100种计算机平台上,适用广泛,从大型机到便携设备,从快速原型创建到大规模可扩展开发. Why? Perl追求简洁快速地解决问题,可很方便 ...

  5. idea 插件的使用 进阶篇

    CSDN 2016博客之星评选结果公布    [系列直播]零基础学习微信小程序!      "我的2016"主题征文活动   博客的神秘功能 idea 插件的使用 进阶篇(个人收集 ...

  6. idea如何设置类头注释和方法注释

    CSDN 2016博客之星评选结果公布      [系列直播]算法与游戏实战技术      "我的2016"主题征文活动 详细:idea如何设置类头注释和方法注释 标签: idea ...

  7. 高性能MySQL之【第十五章 备份与恢复】学习记录

      我们不打算包括的话题:      安全(访问备份,恢复数据的权限,文件是否需要加密)      备份存储在哪里,包括他们应该离源数据多远,以及如何将数据从源头移动到目的地      保留策略.审计 ...

  8. JAVA 集合 List 分组的两种方法

    CSDN日报20170219--<程序员的沟通之痛> [技术直播]揭开人工智能神秘的面纱 程序员1月书讯 云端应用征文大赛,秀绝招,赢无人机! JAVA 集合 List 分组的两种方法 2 ...

  9. Commons BeanUtils 中对Map的操作

    CSDN学院招募微信小程序讲师啦 程序员简历优化指南! [观点]移动原生App开发 PK HTML 5开发 云端应用征文大赛,秀绝招,赢无人机! Commons BeanUtils 中对Map的操作 ...

随机推荐

  1. Unity3D4.* NGUI制作动态字库

    新建一个工程,这个工程必须没有中文路径,否则会不识别字体!!! 首先导入NGUI插件,这里我用的是NGUI 3.0.2版本的. 在Assets 下创建一个文件夹,用来存放接下来的工作文件 . 这里随便 ...

  2. sscanf非常的重要

    刚进研究生阶段,发现曾经学习的c语言真的好少好少,很少能够看见scanf printf等..... 以后实验的80%以上都是在linux下面 老师让我看看关于一个日志会聚的一个项目模块 发现基本上都是 ...

  3. ASP.NET Core MVC 2.x 全面教程_ASP.NET Core MVC 20. Model Binding

    模型绑定 这是个最简单的绑定 先从表单找.再从路由的值上找,最后去QueryString上找 通过反射获取这个类型的属性进行填充.反射完有几个属性,就开始去找着几个属性并绑定值 复杂类型 最后又加了一 ...

  4. 容器云未来:Kubernetes、Istio 和 Knative

    导读 目前以Kubernetes为基础构建的容器生态逐渐完善,这其中Kubernetes.Istio.Knative三个独立项目被越来越多的人提及,并且已经开始尝试大规模落地实践,它们恰好构成了容器云 ...

  5. POJ - 1661 - Help Jimmy - 简单dp

    http://poj.org/problem?id=1661 一般化处理,把一开始的落地和大地都视作平台,设计平台类的属性.dp的时候显然是从上往下dp的,而且要小心Jimmy不能够穿过平台,也就是从 ...

  6. P1648 看守

    传送门 以二维的两个点\((x1,y1),(x2,y2)\)为例,那么他们之间的曼哈顿距离肯定为一下四个之一\((x1-x2)+(y1-y2)\),\((x2-x1)+(y1-y2)\),\((x1- ...

  7. 如何正确访问Redis中的海量数据?服务才不会挂掉!

    一.前言 有时候我们需要知道线上的Redis的使用情况,尤其需要知道一些前缀的key值,让我们怎么去查看呢?并且通常情况下Redis里的数据都是海量的,那么我们访问Redis中的海量数据?如何避免事故 ...

  8. 限制属性绑定(__slots__)

    正常情况下,当定义了一个class并创建实例后,可以给该实例绑定任何属性和方法,这就是动态语言的灵活性 属性和方法是可以直接定义在class中的,但动态绑定允许在程序运行的过程中动态给class加上属 ...

  9. 普通app自动化测试与手游app自动化测试的区别

    [转载] 手游自动化测试与App自动化测试技术上的区别 手游和App的开发技术不同,导致了两者的自动化测试技术完全不同.一般来说,安卓应用是使用Android SDK开发的,利用Java编写.那么在A ...

  10. [Usaco2012 Jan]Video Game

    Description Bessie is playing a video game! In the game, the three letters 'A', 'B', and 'C' are the ...