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. 入口函数WinMain

    int WINAPI WinMain() HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShowCmd ); ...

  2. 【205】C#实现远程桌面访问

    参考:Remote Desktop using C#.NET 参考文件:TscForm.zip 本博客主要是讲述怎样用 .NET 平台中 Microsoft Terminal Services Cli ...

  3. 张高兴的 .NET Core IoT 入门指南:(三)使用 I2C 进行通信

    什么是 I2C 总线 I2C 总线(Inter-Integrated Circuit Bus)是设备与设备间通信方式的一种.它是一种串行通信总线,由飞利浦公司在1980年代为了让主板.嵌入式系统或手机 ...

  4. bzoj 4161 Shlw loves matrixI【常系数线性齐次递推】

    并不会递推,不过板子挺好背的,只要是类似的递推都能用,但是注意c数组不能使负数 如果除了递推还有常数项的话,就用f[i]-f[i-1]的方式消掉常数项(然后多一个f[i-1]的项) #include& ...

  5. IDEA打开父类的接口方法快捷键

    有两个类,分别为A类是接口.B类为A的实现类. 如果想在B类里的某个方法,打开A类 可以鼠标右键点击这个方法名,按下键盘:Ctrl+U,就可以跳转到A类接口的该方法上面.

  6. Robotframework自动化系列:筛选结果数量统计

    Robotframework自动化系统:筛选结果数量统计 上一个节点已经可以随机选中某一个下拉框的值,我们在使用evaluate随机数的时候需要计算下拉选项总数,这时候我们是手工计算输入的:这时候如果 ...

  7. (四)python自带解释器(IDLE)的使用

    什么是IDE? Integrated Development Environment(集成开发环境) 打个不恰当的比方,如果说写代码是制作一件工艺品,那IDE就是机床.再打个不恰当的比方,PS就是图片 ...

  8. 牛客国庆集训派对Day_1~3

    Day_1 A.Tobaku Mokushiroku Kaiji 题目描述 Kaiji正在与另外一人玩石头剪刀布.双方各有一些代表石头.剪刀.布的卡牌,每局两人各出一张卡牌,根据卡牌的内容决定这一局的 ...

  9. 506 Relative Ranks 相对名次

    给出 N 名运动员的成绩,找出他们的相对名次并授予前三名对应的奖牌.前三名运动员将会被分别授予 “金牌”,“银牌” 和“ 铜牌”("Gold Medal", "Silve ...

  10. 492 Construct the Rectangle 构建矩形

    详见:https://leetcode.com/problems/construct-the-rectangle/description/ C++: class Solution { public: ...