ときどきの雑記帖 RE* (新南口)
The Day After
今週のしずえさん
中文(中国語)で「酒店」とは?
その他日本語と中国語で全然異なる意味になるもの
- 上手
- 床
- 机
メモ
oberon+
Code Examples | The Oberon+ Programming Language
module Fibonacci
proc calc*(n : integer): integer
var a, b: integer // comma is optional
begin
if n > 1 then
a := calc(n - 1)
b := calc(n - 2)
return a + b
elsif n = 0 then
return 0
else
return 1
end
end calc
var res: integer
begin
res := calc(21)
assert(res = 10946)
end Fibonacci
Oberon (programming language) - Wikipedia なのに予約語が大文字じゃない?
awk
awkネタ二題。
40周年
AWKが40歳。古い言語は使われなくなる傾向があるけどCとAWKは例外だそうw
— 高梨陣平 (@jingbay) September 7, 2021
GAWKによる拡張や最近のAWKの利用例等紹介 https://t.co/prRUl9kBs2
- Awk: The Power and Promise of a 40-Year-Old Language | Hacker News
- Awk: The Power and Promise of a 40-Year-Old Language
ツイートにあるのは元記事のこの部分からだと思うけど、 なんかニュアンス違わない? (まあ以前からこの人のツイート内容は(元記事の内容と相違があるかもという)多少の疑いを持って見てはいるけど)
Languages don’t enjoy long lives. Very few people still code with the legacies of the 1970s: ML, Pascal, Scheme, Smalltalk. (The C language is still widely used but in significantly updated versions.) Bucking that trend, the 1977 Unix utility Awk can boast of a loyal band of users and seems poised to continue far into the future. In this article, I’ll explain what makes Awk special and keeps it relevant.
FS
[awk] awkコマンド備忘録(随時更新) - Qiita
-F:区切り文字の指定
-F" " //スペース区切り
-F"\t" //タブ区切り
-F"," //カンマ区切り
FS に一個のスペースというのはデフォルト設定と同じなんだけど、 これが単なるスペース区切りとは違うのはマニュアルにもあるはずなんだけどなあ。 この辺→ 4.5.1 Whitespace Normally Separates Fields
kbk@toybox4:/mnt/c/Users/kbk$ echo 'a b c'
a b c
kbk@toybox4:/mnt/c/Users/kbk$ echo 'a b c'|awk '{print $2}'
b
kbk@toybox4:/mnt/c/Users/kbk$ echo 'a b c'|awk -F'[ ]' '{print $2}'
kbk@toybox4:/mnt/c/Users/kbk$ echo 'a b c'|awk -F' ' '{print $2}'
b
kbk@toybox4:/mnt/c/Users/kbk$ echo ' a b c'|awk '{print $2}'
b
kbk@toybox4:/mnt/c/Users/kbk$ echo ' a b c'|awk -F' ' '{print $2}'
b
kbk@toybox4:/mnt/c/Users/kbk$ echo ' a b c'|awk -F'[ ]' '{print $2}'
a
この仕様、PerlやRubyのsplitにも影響を与えていたりする。
awk -v
もうちょっとこの話題(-vオプションの話) を引っ張るのじゃよ。
awk の -v オプションに潜む罠 ~任意の値を渡す時はエスケープ処理が必要です!~ - Qiita
重要 なぜ -v はこのような設計なのか?
この原則に置いて awk スクリプトの動作を制御するにはどうすればよいでしょうか? 言い換えると awk スクリプトに対してコマンドで言う - で始まるオプションのようなものを渡したい場合にどうするか?という話です。 おそらくそれが -v オプションの役目なのでしょう。 つまり任意のデータを渡すのではなく(シェルスクリプトから)決まった値を渡すだけなのでエスケープ処理は不要なのです。
awk本
に-vオプション導入のいきさつが書いてあったような。 という記憶があったのでざっと眺めてみたが見つからず、 その後gawk のマニュアルで発見。
2.3 Other Command-Line Arguments
Any additional arguments on the command line are normally treated as input files to be processed in the order specified. However, an argument that has the form var=value, assigns the value value to the variable var—it does not specify a file at all. (See Assigning Variables on the Command Line.) In the following example, count=1 is a variable assignment, not a file name:
awk -f program.awk file1 count=1 file2
As a side point, should you really need to have awk process a file named count=1 (or any file whose name looks like a variable assignment), precede the file name with ‘./’, like so:
awk -f program.awk file1 ./count=1 file2
All the command-line arguments are made available to your awk program in the ARGV array (see section Predefined Variables). Command-line options and the program text (if present) are omitted from ARGV. All other arguments, including variable assignments, are included. As each element of ARGV is processed, gawk sets ARGIND to the index in ARGV of the current element. (gawk makes the full command line, including program text and options, available in PROCINFO[“argv”]; see section Built-in Variables That Convey Information.)
The variable values given on the command line are processed for escape sequences (see section Escape Sequences). (d.c.)
In some very early implementations of awk, when a variable assignment occurred before any file names, the assignment would happen before the BEGIN rule was executed. awk’s behavior was thus inconsistent; some command-line assignments were available inside the BEGIN rule, while others were not. Unfortunately, some applications came to depend upon this “feature.” When awk was changed to be more consistent, the -v option was added to accommodate applications that depended upon the old behavior.
The variable assignment feature is most useful for assigning to variables such as RS, OFS, and ORS, which control input and output formats, before scanning the data files. It is also useful for controlling state if multiple passes are needed over a data file. For example:
awk ‘pass == 1 { pass 1 stuff }
pass == 2 { pass 2 stuff }’ pass=1 mydata pass=2 mydata
もうひとつ
のPassing Parametters Into a Script
にも同様のことが書かれていた(記憶にあったのはこれだったかも)。
また、その脚注によれば nawk の最初からあったわけではなかったらしい。
The -v option was not part of the original(1987) version pf nawk (still used on SinOS 4.1.x systems and some System V Release 3.x systems). It was added in 1989 after Brian Kernighan of Bell Labs, the GNU awk authours, and the authours of MKS awk agreed on a way to set variables on the command line that would be available inside the BEGIN block. It is now part of the POSIX specification for awk.
ちなみにone true awk の関連するコードはこの辺。
lib.c/getrec
while (argno < *ARGC || infile == stdin) {
dprintf( ("argno=%d, file=|%s|\n", argno, file) );
if (infile == NULL) { /* have to open a new file */
file = getargv(argno);
if (file == NULL || *file == '\0') { /* deleted or zapped */
argno++;
continue;
}
if (isclvar(file)) { /* a var=value arg */
setclvar(file);
argno++;
continue;
}
*FILENAME = file;
dprintf( ("opening file %s\n", file) );
if (*file == '-' && *(file+1) == '\0')
infile = stdin;
else if ((infile = fopen(file, "r")) == NULL)
FATAL("can't open file %s", file);
setfval(fnrloc, 0.0);
}
V7 から後の時代のものへと追いかけていくと (V9にはない?)、
- https://minnie.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/awk
- https://minnie.tuhs.org/cgi-bin/utree.pl?file=V8/usr/src/cmd/awk
- https://minnie.tuhs.org/cgi-bin/utree.pl?file=V10/cmd/awk
V10で以下の部分を発見した(V8の時点 では見つからず)。
case 'v': /* -v a=1 to be done NOW. one -v for each */
コード読み
しばらく放置してるなあ
git
glob zsh
if-then-else
新山さんのツイートから。
"if-then-else"という表現は発明だった。通常の英語では、elseを接続詞として使うことはできない。初期のプログラミング言語では "if not" とか "otherwise" などと書かれていたが、ALGOL60の時代には"else"が一般化していた。しかし最初に使いだした人物とその理由は不明。https://t.co/TYJgdIIuip
— 新山祐介 (Yusuke Shinyama) (@mootastic) September 11, 2021
if-then-else/if-then-else.md at master · e-n-f/if-then-else
非常に面白い内容だった。 これはHacker Newsでも取り上げられていた(過去にもすでに)。
そう言えば、FORTRANの算術IFみたいな三方向分岐はどこから湧いて出たんだろう?
Hugoメモ
表示幅