Skip to Content
『Learning Perl, 8th Edition』
book

『Learning Perl, 8th Edition』

by Randal L. Schwartz, brian d foy, Tom Phoenix
March 2025
Beginner to intermediate
398 pages
5h 33m
Japanese
O'Reilly Media, Inc.
Content preview from 『Learning Perl, 8th Edition』

第6章. ハッシュ

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

この章では、Perlを本当に偉大なプログラミング言語のひとつにしている機能、ハッシュについて説明する。 ハッシュは強力で便利な機能だが、他の強力な言語を何年も使っている人は、ハッシュという言葉を聞いたことがないかもしれない。しかし、これからあなたが書くPerlのプログラムには必ずと言っていいほどハッシュを使うことになるだろう。

ハッシュとは何か?

ハッシュはデータ構造であり、任意の数の値を保持し、自由に取り出すことができるという点では配列と変わらない。しかし、配列のように数値でインデックスを付けるのではなく、名前でハッシュ値を検索する。つまり、キーと呼ばれるインデックスは数字ではなく、任意の一意な文字列である(図6-1参照)。

ハッシュキーはまず文字列なので、配列から要素番号3 を取得する代わりに、例えばwilma というハッシュ要素にアクセスする。

これらのキーは任意の文字列であり、ハッシュ・キーには任意の文字列式を使うことができる。3 という番号の配列要素がひとつしかないように、wilma という名前のハッシュ要素もひとつしかない。

ハッシュを考えるもう一つの方法は、データの樽のようなもので、それぞれのデータにはタグが付いている。樽の中に手を入れてタグを引き抜けば、どんなデータが添付されているかを見ることができる。しかし、樽の中に「最初の」項目はない。配列では、要素0 、要素1 、要素2 、といった具合に始まる。しかし、ハッシュには決まった順番もなければ、最初の要素もない。単なるキーと値のペアの集まりだ。

図6-1. ハッシュのキーと値

キーも値も任意のスカラーだが、キーは常に文字列への変換が行われる。したがって、数値式50/20 をキーとすると、図6-2に示すキーの1つである3文字列"2.5" に変換される。

図6-2. データの樽としてのハッシュ

いつものように、Perlの「不必要な制限はない」という哲学が適用される。ハッシュは、キーと値のペアがゼロの空のハッシュから、メモリをいっぱいにするものまで、どんなサイズでも構わない。

ハッシュの実装の中には(ラリーがアイデアを借用したオリジナルのawk言語など)、ハッシュが大きくなるにつれて処理速度が遅くなるものがある。Perlではそのようなことはなく、効率的でスケーラブルなアルゴリズムを採用している。そのため、ハッシュのキーと値のペアが3つしかない場合、"樽の中に手を入れて "どれか1つを取り出すのはとても速い。ハッシュが300万のキー・バリュー・ペアを持つなら、そのうちのどれかを取り出すのも同じくらい速いはずだ。大きなハッシュは恐れるに足りない。

同じ値を複数回使用することはできるが、キーは常に一意であることを再度述べておく。ハッシュの値は、すべて数字でも、すべて文字列でも、undef、あるいはその混合でもよい。しかし、キーはすべて任意の一意な文字列である。

なぜハッシュを使うのか? ...

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

Learning Perl, 8th Edition

Learning Perl, 8th Edition

Randal L. Schwartz, brian d foy, Tom Phoenix
The AWK Programming Language, 2nd Edition

The AWK Programming Language, 2nd Edition

Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger

Publisher Resources

ISBN: 9798341636231