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』

第14章. 文字列とソート

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

本書の冒頭で述べたように、Perlは90%がテキスト処理、10%がそれ以外の処理であるようなプログラミングの問題を解決するのに適しているように設計されている。だからPerlが強力なテキスト処理能力を持っているのは、正規表現を使ったことがなくても驚くことではない。時には正規表現エンジンが派手すぎて、文字列を扱うもっとシンプルな方法が必要になることもある。

インデックスで部分文字列を発見する

部分文字列の発見は、それをどこで見失ったかによる。たまたま大きな文字列の中で見失ってしまった場合は、index 関数が助けてくれる。こんな感じだ:

my $where = index($big, $small);

Perlは、大きな文字列の中で小さな文字列が最初に出現する場所を探し、最初の文字の位置を整数で返す。文字列の先頭で部分文字列が発見された場合、index は0 を返し、1文字後に発見された場合は1 を返す。もしindex が部分文字列をまったく発見できなければ、-1 を返す。この例では、wor が始まる位置なので、$where は6 を返す:

my $stuff = "Howdy world!";
my $where = index($stuff, "wor");

位置番号の別の考え方は、部分文字列に到達する前にスキップする文字数である。$where は6 なので、wor を発見する前に、$stuff の最初の6文字をスキップしなければならないことがわかる。

index 関数は常に、最初に発見された部分文字列の位置を報告する。しかし、オプションの第3パラメータを使用することで、文字列の先頭よりも後の位置から検索を開始するように指示することができる。index :

my $stuff  = "Howdy world!";
my $where1 = index($stuff, "w");               # $where1 gets 2
my $where2 = index($stuff, "w", $where1 + 1);  # $where2 gets 6
my $where3 = index($stuff, "w", $where2 + 1);  # $where3 gets -1 (not found)

この3番目のパラメータは、事実上戻り値の最小値を与えている。部分文字列がその位置以降にない場合、index は-1 を返す。ループなしでこれを行うことはないだろう。この例では、位置を格納するために配列を使っている:

use v5.10;

my $stuff  = "Howdy world!";

my @where = ();
my $where = -1;
while( 1 ) {
  $where = index( $stuff, 'w', $where + 1 );
  last if $where == -1;
  push @where, $where;
}
say "Positions are @where";

$where を-1 に初期化するのは、index に開始位置を渡す前に1 を追加するからである。これは、最初のパスが特殊化ではないことを意味する。

たまに、部分文字列の最後に出現した文字列を取得したい場合がある。 rindex 、文字列の最後からスキャンを開始する。この例では、最後のスラッシュを発見することができる。スラッシュは文字列の位置 ...

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