Skip to content

Commit

Permalink
append some comment and debug sources
Browse files Browse the repository at this point in the history
  • Loading branch information
tetsu-nagoya committed Mar 25, 2016
1 parent d8eaf9c commit 8f70ba7
Showing 1 changed file with 66 additions and 6 deletions.
72 changes: 66 additions & 6 deletions perlguidetokai01.md
Expand Up @@ -251,7 +251,7 @@ print("a" x 3, "\n"); # => aaa が表示される
* 文字列連結: `.` (ドット)
* 文字列,数値 → 文字列
* 文字列の繰り返し: `x`

### 関数呼び出し

`print()` について少し解説します.
Expand Down Expand Up @@ -956,9 +956,11 @@ my ($line1, $line2) = <>;
リストから数に対応する人名を表示するプログラムを書いてください.
例えば,入力された番号が 1, 2, 4, 2 だとすると,fread, betty, dino,
betty となります.

fred betty barney dino wilma pebbles bamm-bamm

(ヒント: 入力を終了するには,MacOSX,Linux では Control+D,Windows ではControl+Zを押してEnterを入力する必要があります.)

---

# まとめ
Expand Down Expand Up @@ -1105,7 +1107,7 @@ if ("a") {
* `and`, `or`, `&&`, `||`
* ブーリアン → ブーリアン
* `not`, `!`

## 関数

関数は,`関数名 + "(" + 引数1 + ", " + 引数2 ... ")"` の形式で呼び出す.
Expand All @@ -1131,7 +1133,7 @@ if ("a") {
`push()`

: リスト変数の末尾に値を追加する.

`split()`

: 文字列を区切りで区切って,リスト値にして返す.
Expand Down Expand Up @@ -1231,6 +1233,8 @@ print("tetsu\n17\n");

`print` は,`print()` 関数のことで,何も指定しないとデフォルトで,`$_` 変数の内容を出力することになっています.

`ex3_4.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1257,6 +1261,8 @@ foreach (@lines) {
出てきた演算子は,`+`, `-`, `*`, `/`, `.`, `x` です.
それぞれを使ってみて,演算子に慣れましょう.

`ex4_1.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1277,6 +1283,8 @@ print(1 + 2 * 3, "\n"); # => 7

変数を使うときに最初に,`my` を入れるのを忘れないでください.

`ex4_2.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand Down Expand Up @@ -1332,6 +1340,8 @@ close($fh) or die;

半径を固定値ではなく,入力できるようにしたものです.

`ex7_2.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1356,6 +1366,8 @@ print("半径: ", $r, " の円周は ", 2 * $pi * $r, "\n");
`Argument "a" isn't numeric in ...` のような警告が出てしまうでしょう.
これらを回避する方法もありますが,一旦は無視します.

`ex7_3.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1378,6 +1390,8 @@ print($op1 x $op2, "\n");

### 4.

`ex7_4.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand Down Expand Up @@ -1407,6 +1421,8 @@ close($wfh) or die;

まっすぐ書くとこのようになります.

`ex8_1.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1422,6 +1438,8 @@ close($fh) or die;

ファイル名を指定できるようにすると,以下のようになります.

`ex8_1_2.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1440,6 +1458,8 @@ close($fh) or die;

### 2.

`ex8_2.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1455,6 +1475,8 @@ while ($i <= 10) {

### 3.

`ex8_3.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1472,6 +1494,8 @@ print($sum, "\n");

### 4.

`ex8_4.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1492,6 +1516,8 @@ print($sum, "\n");

### 5.

`ex8_5.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1509,6 +1535,8 @@ while ($n < 20) {

素直に書くとこのようになるでしょう.

`ex8_6.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1519,6 +1547,7 @@ my $input = <STDIN>;
chomp($input);
while ($input ne "quit") {
$input = <STDIN>;
chomp($input);
}
print("end...", "\n");
```
Expand All @@ -1527,6 +1556,8 @@ print("end...", "\n");
スカラ変数が初期化時には,undef であることを利用して,
以下のように書くこともできます.

`ex8_6_2.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1536,6 +1567,7 @@ use warnings;
my $input;
while (!defined($input) or $input ne "quit") {
$input = <STDIN>;
chomp($input);
}
print("end...", "\n");
```
Expand All @@ -1544,6 +1576,8 @@ print("end...", "\n");

### 1.

`ex9_1.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1558,8 +1592,26 @@ while ($i < 5) {
}
```

`foreach` を使うこともできます.

`ex9_1_2.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

use strict;
use warnings;

my @chars = ("alpha", "bravo", "charlie", "delta", "echo");
foreach my $char (@chars) {
print($char, "\n");
}
```

### 2.

`ex9_2.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1582,6 +1634,8 @@ print(2 * $pi * $r, "\n");

### 4.

`ex9_4.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1601,6 +1655,8 @@ print($sum, "\n");

### 5.

`ex9_5.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1624,6 +1680,8 @@ print($sum, "\n");

### 1.

`ex10_1.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1633,7 +1691,7 @@ use warnings;
# 入力
my $ymd = <STDIN>;
chomp($ymd);
my ($y, $m, $d) = split(/,/, $in);
my ($y, $m, $d) = split(/,/, $ymd);

# 出力
print($y, "", $m, "", $d, "日です", "\n");
Expand All @@ -1644,6 +1702,8 @@ print($y, "年", $m, "月", $d, "日です", "\n");
数値を読み込んで,配列の添字に使うことがわかればできます.
リストの添字は,`0`から始まるので,入力する数字と1つずれていることに注意が必要です.

`ex10_2.pl`

```{.perl .numberLines}
#!/usr/bin/env perl

Expand All @@ -1659,7 +1719,7 @@ while (defined(my $n = <STDIN>)) {
}

foreach my $index (@indices) {
print $names[$index]; # 添字とズレているので注意
print $names[$index], "\n"; # 添字とズレているので注意
}
```

Expand Down

0 comments on commit 8f70ba7

Please sign in to comment.