Skip to Content
プログラミング言語AWK 第2版
book

プログラミング言語AWK 第2版

by Alfred V. Aho, Brian W. Kernighan, Peter J. Weinberger, 千住 治郎
May 2024
Intermediate to advanced
276 pages
3h 51m
Japanese
O'Reilly Japan, Inc.
Content preview from プログラミング言語AWK 第2版

6章テキスト処理

本章で例示するプログラムには共通テーマがある。自然言語のテキスト操作だ。単語や文章をランダムに生成する、限定的ながらもユーザと対話する、などのテキスト処理だ。大半のプログラムは説明を目的としているため、実用性は高くなく玩具程度かもしれないが、ドキュメント執筆支援プログラムとして実用されているものもある。

6.1ランダムなテキスト生成

ランダムデータ生成プログラムには多様な用途がある。Awk では疑似乱数を 1 つ返す組み込み関数、rand が核となる。rand 関数は種 (seed、疑似乱数生成の初期値) から乱数を生成するため、異なる乱数シーケンスが欲しければ、srand(n) を一度コールし、rand を種 n で初期化する必要がある。srand の引数を省略するとディフォルトで現在時刻が用いられる。srand の戻り値は前回使用した種なので、これを用いれば乱数シーケンスを再現できる。

ランダムな選択

rand が返す戻り値は 0 以上 1 未満の浮動小数点数だが、欲しい乱数は 1 から n までの整数という場合が多い。rand の戻り値から整数乱数を算出するのは容易だ。

例6-1 randint関数
# randint - return random integer k, 1 <= k <= n

function randint(n) {
    return int(n * rand()) + 1
}

(コメント訳)

randint – 整数乱数k、1 <= k <= n

上例の randint(n)rand から得た浮動小数点数を 0 から n の範囲に収まるよう乗算する。小数点以下を切り捨て、0 から n-1 の範囲の整数とし、1 を加える。

例6-1randint ...

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

プログラミングRust 第2版

プログラミングRust 第2版

Jim Blandy, Jason Orendorff, Leonora F. S. Tindall, 中田 秀基
仕事ではじめる機械学習 第2版

仕事ではじめる機械学習 第2版

有賀 康顕, 中山 心太, 西林 孝
詳説 イーサネット 第2版

詳説 イーサネット 第2版

Charles E. Spurgeon, Joann Zimmerman, 三浦 史光, 豊沢 聡
Linuxカーネルプログラミング 第2版

Linuxカーネルプログラミング 第2版

Kaiwan N. Billimoria, 武内 覚, 大岩 尚宏

Publisher Resources

ISBN: 9784814400706Publisher Website