Skip to Content
Perl语言入门
book

Perl语言入门

by Randal L. Schwartz, brian d foy, Tom Phoenix
August 2018
Beginner to intermediate
382 pages
7h 14m
Chinese
Southeast University Press
Content preview from Perl语言入门
115
6
哈希
本章我们会看到
Perl
成为杰出编程语言的关键特色——哈希。尽管哈希非常强大有
用,但那些多年使用其他语言的人却可能从未听说。不过从现在开始,几乎每个
Perl
程序中你都会用到哈希。是的,它真的非常重要。
什么是哈希?
哈希是一种数据结构,它和数组的相似之处在于可以容纳任意多的值,并可按需取
用,而它和数组的不同之处在于检索方式不同,数组是以数字下标来检索,哈希则以
唯一的名字来检索。我们把这个唯一的名字称为键(
key
)(参阅
6-1
)。
哈希的键只能使用普通的字符串表示。类似于通过数字
3
获取数组元素,我们可以
wilma
这个名字来存取哈希元素。
这些键可以是任何字符串——你可以用任意字符串表达式作为哈希键。它们也必须
是唯一的字符串,就像数组只能有一个编号为
3
的元素一样,哈希也只能有一个名
wilma
的元素。
我们可以把哈希想象成一大桶数据,其中每个数据都有关联的标签。你可以伸手到桶
里任意取出一张标签,看它上面写的数据是什么。但是桶里没有“第一个”元素,只
有一堆元素。在数组里,第一个为元素
0
,然后是元素
1
2
等等,但哈希里没有顺
序,因此也没有所谓的第一个元素,有的只是一堆键
-
值对的集合。
116
6
6
-
1
:哈希的键与值
这些键和值都是任意的标量,但键总会被转换成字符串。假如你以数字表达 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

计算机科学导论:跨学科方法

计算机科学导论:跨学科方法

罗伯特 塞奇威克, 凯文 韦恩
Perl语言入门(第8版)

Perl语言入门(第8版)

Randal L. Schwartz, brian d foy, Tom Phoenix

Publisher Resources

ISBN: 9787564177911