=encoding euc-jp =head1 NAME =begin original perllol - Manipulating Arrays of Arrays in Perl =end original perllol - Perl で配列の配列を操作する =head1 DESCRIPTION =head2 Declaration and Access of Arrays of Arrays (配列の配列の定義とアクセス) =begin original The simplest two-level data structure to build in Perl is an array of arrays, sometimes casually called a list of lists. It's reasonably easy to understand, and almost everything that applies here will also be applicable later on with the fancier data structures. =end original Perl で構築する一番簡単なデータ構造は、配列の配列(カジュアルに リストのリストとも呼ばれることがあります)です。 これは理解しやすく、そしてより複雑なデータ構造に対しても 適用することのできるものです。 =begin original An array of an array is just a regular old array @AoA that you can get at with two subscripts, like C<$AoA[3][2]>. Here's a declaration of the array: =end original 配列の配列は、通常の古い配列 @AoA のようなものです; これは C<$AoA[3][2]> のように、二つの添え字で要素を取得することができます。 配列の宣言の例を挙げましょう。 use 5.010; # so we can use say() =begin original # assign to our array, an array of array references @AoA = ( [ "fred", "barney", "pebbles", "bambam", "dino", ], [ "george", "jane", "elroy", "judy", ], [ "homer", "bart", "marge", "maggie", ], ); say $AoA[2][1]; bart =end original # 配列に配列へのリファレンスの配列を代入する @AoA = ( [ "fred", "barney", "pebbles", "bambam", "dino", ], [ "george", "jane", "elroy", "judy", ], [ "homer", "bart", "marge", "maggie", ], ); say $AoA[2][1]; bart =begin original Now you should be very careful that the outer bracket type is a round one, that is, a parenthesis. That's because you're assigning to an @array, so you need parentheses. If you wanted there I to be an @AoA, but rather just a reference to it, you could do something more like this: =end original このとき、外側の括弧が丸括弧であったことに注意すべきです。 これは、上の例では@配列に代入するので丸括弧を使う必要があったためなのです。 もし @AoA ではなくて、単にリファレンスを代入したかったというのであれば、 次のように書くことができます: =begin original # assign a reference to array of array references $ref_to_AoA = [ [ "fred", "barney", "pebbles", "bambam", "dino", ], [ "george", "jane", "elroy", "judy", ], [ "homer", "bart", "marge", "maggie", ], ]; say $ref_to_AoA->[2][1]; bart =end original # 配列へのリファレンスの配列へのリファレンスを代入する $ref_to_AoA = [ [ "fred", "barney", "pebbles", "bambam", "dino", ], [ "george", "jane", "elroy", "judy", ], [ "homer", "bart", "marge", "maggie", ], ]; say $ref_to_AoA->[2][1]; bart =begin original Notice that the outer bracket type has changed, and so our access syntax has also changed. That's because unlike C, in perl you can't freely interchange arrays and references thereto. $ref_to_AoA is a reference to an array, whereas @AoA is an array proper. Likewise, C<$AoA[2]> is not an array, but an array ref. So how come you can write these: =end original 外側の括弧が変わったことと、アクセスの構文が変わっているということに 注目してください。 これは C とは違って、Perl では配列と参照とを自由に交換できないからです。 $ref_to_AoA は配列への参照です; その配列は @AoA で、これがまた配列です。 同様に、C<$AoA[2]> は配列ではなく配列への参照です。 ですから: $AoA[2][2] $ref_to_AoA->[2][2] =begin original instead of having to write these: =end original これは、以下のような書き方でも同じことになります: $AoA[2]->[2] $ref_to_AoA->[2]->[2] =begin original Well, that's because the rule is that on adjacent brackets only (whether square or curly), you are free to omit the pointer dereferencing arrow. But you cannot do so for the very first one if it's a scalar containing a reference, which means that $ref_to_AoA always needs it. =end original この規則は隣り合ったかっこ(それが大かっこだろうが中かっこだろうが) だけのものなので、参照外しをする矢印を自由に省略できます。 けれども一番最初にある矢印だけは、それがリファレンスを保持する スカラであるために省略することはできません; これは $ref_to_AoA が常に 必要とするものです。 =head2 Growing Your Own (大きくする) =begin original That's all well and good for declaration of a fixed data structure, but what if you wanted to add new elements on the fly, or build it up entirely from scratch? =end original 固定的なデータ構造の宣言は良いのですが、その場で新しい要素を 追加したいとき、あるいは完全に 0 から作り上げたいときにはどうするのでしょう? =begin original First, let's look at reading it in from a file. This is something like adding a row at a time. We'll assume that there's a flat file in which each line is a row and each word an element. If you're trying to develop an @AoA array containing all these, here's the right way to do that: =end original まず最初に、ファイルから読み込むことを見てみましょう。 これは一度に一つの行を追加していくようなものです。 私たちはここで、読み込んでいるファイルが、一行(line)が一つの行(row)に 対応し、各単語が要素に対応しているようなフラットなファイルであると 仮定しています。 もし配列 @AoA にそういった物を設定しようとするのであれば、 それは以下のようなやり方になります: while (<>) { @tmp = split; push @AoA, [ @tmp ]; } =begin original You might also have loaded that from a function: =end original 関数を使って読み込むこともできます: for $i ( 1 .. 10 ) { $AoA[$i] = [ somefunc($i) ]; } =begin original Or you might have had a temporary variable sitting around with the array in it. =end original あるいは、配列に設定するために使う一時変数を使うこともできます。 for $i ( 1 .. 10 ) { @tmp = somefunc($i); $AoA[$i] = [ @tmp ]; } =begin original It's important you make sure to use the C<[ ]> array reference constructor. That's because this wouldn't work: =end original 配列への参照のコンストラクタである C<[ ]> を使うことが重要です。 次のように書いてしまうのはとてもまずいやりかたです: $AoA[$i] = @tmp; # WRONG! =begin original The reason that doesn't do what you want is because assigning a named array like that to a scalar is taking an array in scalar context, which means just counts the number of elements in @tmp. =end original あなたが望んでいることをしない理由は、スカラに対するような名前付き配列の 代入は、配列をスカラコンテキストとして扱い、これは単に @tmp の要素の数を 数えることになるからです。 =begin original If you are running under C (and if you aren't, why in the world aren't you?), you'll have to add some declarations to make it happy: =end original C の元で実行するのであれば、(そしてもししていないのなら、 どうしてしていないの?) 以下の様にちょっと宣言を付け加えるとよいでしょう: use strict; my(@AoA, @tmp); while (<>) { @tmp = split; push @AoA, [ @tmp ]; } =begin original Of course, you don't need the temporary array to have a name at all: =end original もちろん、一時的な配列もなければならないというものではありません: while (<>) { push @AoA, [ split ]; } =begin original You also don't have to use push(). You could just make a direct assignment if you knew where you wanted to put it: =end original また、push() を使わなくてもできます。 どこに押し込めたいかと言うことがわかっているのなら、直接 代入させることもできます: my (@AoA, $i, $line); for $i ( 0 .. 10 ) { $line = <>; $AoA[$i] = [ split " ", $line ]; } =begin original or even just =end original あるいはこういう風にもできます: my (@AoA, $i); for $i ( 0 .. 10 ) { $AoA[$i] = [ split " ", <> ]; } =begin original You should in general be leery of using functions that could potentially return lists in scalar context without explicitly stating such. This would be clearer to the casual reader: =end original 本当にそうしたいときを除き、スカラコンテキストでリストを返すかもしれない 関数を使ってしまう可能性に気をつけるべきです。 これは普通の読み手には明らかでしょう: my (@AoA, $i); for $i ( 0 .. 10 ) { $AoA[$i] = [ split " ", scalar(<>) ]; } =begin original If you wanted to have a $ref_to_AoA variable as a reference to an array, you'd have to do something like this: =end original 配列へのリファレンスとして変数 $ref_to_AoA を使いたいというのであれば、 以下の様にする必要があるでしょう: while (<>) { push @$ref_to_AoA, [ split ]; } =begin original Now you can add new rows. What about adding new columns? If you're dealing with just matrices, it's often easiest to use simple assignment: =end original これで新しい行を追加することができます。 新しいカラムを追加するのは? あなたがまさに行列を扱っているのなら、大概は単純な代入となります: for $x (1 .. 10) { for $y (1 .. 10) { $AoA[$x][$y] = func($x, $y); } } for $x ( 3, 7, 9 ) { $AoA[$x][20] += func2($x); } =begin original It doesn't matter whether those elements are already there or not: it'll gladly create them for you, setting intervening elements to C as need be. =end original これは対象となる要素が既に存在しているかどうかには影響されません: (ない場合でも)喜んであなたのためにその要素を作り出し、必要に応じて 間にある要素に C をセットします。 =begin original If you wanted just to append to a row, you'd have to do something a bit funnier looking: =end original あなたは、単に行に追加したいだけという場合であっても、 ちょっと妙に見えることをしなければならないでしょう: =begin original # add new columns to an existing row push @{ $AoA[0] }, "wilma", "betty"; # explicit deref =end original # add new columns to an existing row push @{ $AoA[0] }, "wilma", "betty"; # 明示的デリファレンス =begin original Prior to Perl 5.14, this wouldn't even compile: =end original Perl 5.14 以前では、これはコンパイルも出来ませんでした: =begin original push $AoA[0], "wilma", "betty"; # implicit deref =end original push $AoA[0], "wilma", "betty"; # 暗黙的デリファレンス =begin original How come? Because once upon a time, the argument to push() had to be a real array, not just a reference to one. That's no longer true. In fact, the line marked "implicit deref" above works just fine--in this instance--to do what the one that says explicit deref did. =end original なぜでしょうか? それは以前は push() の引数は参照ではなく、実際の配列である必要が あったからです。 これはもはや正しくはありません。 実際、上述の「暗黙的デリファレンス」と書かれた行は -- この場合は -- 正しく動作します; デリファレンスが行われます。 =begin original The reason I said "in this instance" is because that I works because C<$AoA[0]> already held an array reference. If you try that on an undefined variable, you'll take an exception. That's because the implicit derefererence will never autovivify an undefined variable the way C<@{ }> always will: =end original 「この場合は」と言った理由は、C<$AoA[0]> が既に配列リファレンスを 保持している場合 I<にのみ> 動作するからです。 未定義値に対して行おうとすると、例外が発生します。 これは、暗黙的デリファレンスは、C<@{ }> が常に行うような未定義値の 自動有効化を行わないからです: my $aref = undef; push $aref, qw(some more values); # WRONG! push @$aref, qw(a few more); # ok =begin original If you want to take advantage of this new implicit dereferencing behavior, go right ahead: it makes code easier on the eye and wrist. Just understand that older releases will choke on it during compilation. Whenever you make use of something that works only in some given release of Perl and later, but not earlier, you should place a prominent =end original この新しい暗黙的デリファレンスの振る舞いの利点を使いたいなら、正しく 前に進んでください: これにより目と手に優しいコードになります。 単に古いリリースではコンパイル中にエラーが出ることを理解してください。 特定のリリース以降の Perl 以降でのみ動作し、それより前では動作しないような ものを使う時はいつでも、以下のように目立つように: use v5.14; # needed for implicit deref of array refs by array ops =begin original directive at the top of the file that needs it. That way when somebody tries to run the new code under an old perl, rather than getting an error like =end original ファイルの先頭にこの指示子を書くべきです。 これにより、誰かが新しいコードを古い perl で実行しようとしたときに、 次のようなエラーではなく: Type of arg 1 to push must be array (not array element) at /tmp/a line 8, near ""betty";" Execution of /tmp/a aborted due to compilation errors. =begin original they'll be politely informed that =end original 次のように親切な情報が出力されます: Perl v5.14.0 required--this is only v5.12.3, stopped at /tmp/a line 1. BEGIN failed--compilation aborted at /tmp/a line 1. =head2 Access and Printing (アクセスと表示) =begin original Now it's time to print your data structure out. How are you going to do that? Well, if you want only one of the elements, it's trivial: =end original こんどはこのデータ構造を出力する番です。 あなたはどうやろうと考えてますか? そうですね、簡単に要素を一つだけ出力したいとするとこうなります: print $AoA[0][0]; =begin original If you want to print the whole thing, though, you can't say =end original 配列の内容全部を出力したいとき、次のようには書けません。 =begin original print @AoA; # WRONG =end original print @AoA; # 間違い =begin original because you'll get just references listed, and perl will never automatically dereference things for you. Instead, you have to roll yourself a loop or two. This prints the whole structure, using the shell-style for() construct to loop across the outer set of subscripts. =end original なぜなら、これでは単にリストへのリファレンスが取れるだけで、 perl はそれを自動的に参照外しするようなことはしないからです。 このため、あなたは自分自身でループしなければなりません。 これは外側の添え字に対するループでシェルスタイルの for() を使って 構造全体を出力します。 for $aref ( @AoA ) { say "\t [ @$aref ],"; } =begin original If you wanted to keep track of subscripts, you might do this: =end original 添え字を記録したいのなら、このようにできます: for $i ( 0 .. $#AoA ) { say "\t elt $i is [ @{$AoA[$i]} ],"; } =begin original or maybe even this. Notice the inner loop. =end original あるいはこのようなやり方もあります。 内側のループに注目してください。 for $i ( 0 .. $#AoA ) { for $j ( 0 .. $#{$AoA[$i]} ) { say "elt $i $j is $AoA[$i][$j]"; } } =begin original As you can see, it's getting a bit complicated. That's why sometimes is easier to take a temporary on your way through: =end original 見て判るようにこれは少々複雑です。 しかし、途中で一時変数を使えば簡単にできます: for $i ( 0 .. $#AoA ) { $aref = $AoA[$i]; for $j ( 0 .. $#{$aref} ) { say "elt $i $j is $AoA[$i][$j]"; } } =begin original Hmm... that's still a bit ugly. How about this: =end original うーんまだちょっと見にくいですね。 これでどうでしょう: for $i ( 0 .. $#AoA ) { $aref = $AoA[$i]; $n = @$aref - 1; for $j ( 0 .. $n ) { say "elt $i $j is $AoA[$i][$j]"; } } =begin original When you get tired of writing a custom print for your data structures, you might look at the standard L or L modules. The former is what the Perl debugger uses, while the latter generates parsable Perl code. For example: =end original あなたのデータ構造のためのカスタム print を書くのに疲れたなら、 標準の L モジュールや L モジュールを 見るとよいでしょう。 前者は Perl デバッガが使っていて、後者は Perl コードとしてパース可能なものを 出力します。 例えば: use v5.14; # using the + prototype, new to v5.14 sub show(+) { require Dumpvalue; state $prettily = new Dumpvalue:: tick => q("), compactDump => 1, # comment these two lines out veryCompact => 1, # if you want a bigger dump ; dumpValue $prettily @_; } # Assign a list of array references to an array. my @AoA = ( [ "fred", "barney" ], [ "george", "jane", "elroy" ], [ "homer", "marge", "bart" ], ); push $AoA[0], "wilma", "betty"; show @AoA; =begin original will print out: =end original これは以下のものを出力します: 0 0..3 "fred" "barney" "wilma" "betty" 1 0..2 "george" "jane" "elroy" 2 0..2 "homer" "marge" "bart" =begin original Whereas if you comment out the two lines I said you might wish to, then it shows it to you this way instead: =end original 一方、そうしたいかもしれないと書いた二つの行をコメントアウトすると、 代わりに以下のようなものが出力されます: 0 ARRAY(0x8031d0) 0 "fred" 1 "barney" 2 "wilma" 3 "betty" 1 ARRAY(0x803d40) 0 "george" 1 "jane" 2 "elroy" 2 ARRAY(0x803e10) 0 "homer" 1 "marge" 2 "bart" =head2 Slices (スライス) =begin original If you want to get at a slice (part of a row) in a multidimensional array, you're going to have to do some fancy subscripting. That's because while we have a nice synonym for single elements via the pointer arrow for dereferencing, no such convenience exists for slices. =end original 多次元配列のスライス(行部分)を取りたいのであれば、ややおかしな添え字付けを する必要があるでしょう。 これは参照外しのための参照の矢印を使った単一の要素に対するものは あるのですが、それに対応するスライス用の便利なものはないのです。 =begin original Here's how to do one operation using a loop. We'll assume an @AoA variable as before. =end original 以下は、ループを使った一つの操作をどのように行うかの例です。 変数 @AoA が前のものと同じであると仮定しています。 @part = (); $x = 4; for ($y = 7; $y < 13; $y++) { push @part, $AoA[$x][$y]; } =begin original That same loop could be replaced with a slice operation: =end original このループをスライス演算に置き換えることができます: @part = @{$AoA[4]}[7..12]; =begin original or spaced out a bit: =end original または少し空白を入れると: @part = @{ $AoA[4] } [ 7..12 ]; =begin original But as you might well imagine, this can get pretty rough on the reader. =end original あなたも見て感じるかもしれませんが、これは読み手にはちょっと不親切です。 =begin original Ah, but what if you wanted a I, such as having $x run from 4..8 and $y run from 7 to 12? Hmm... here's the simple way: =end original あー、でも、$x を 4..8、$y を 7 から 12 とするような I<二次元のスライス> を 必要とするときには? うーん、単純なやり方はこうでしょう。 @newAoA = (); for ($startx = $x = 4; $x <= 8; $x++) { for ($starty = $y = 7; $y <= 12; $y++) { $newAoA[$x - $startx][$y - $starty] = $AoA[$x][$y]; } } =begin original We can reduce some of the looping through slices =end original スライスを使ってループを簡単にできます: for ($x = 4; $x <= 8; $x++) { push @newAoA, [ @{ $AoA[$x] } [ 7..12 ] ]; } =begin original If you were into Schwartzian Transforms, you would probably have selected map for that =end original あなたがシュワルツ変換に興味を持っているのなら、map を使って次のように することを選ぶかもしれません: @newAoA = map { [ @{ $AoA[$_] } [ 7..12 ] ] } 4 .. 8; =begin original Although if your manager accused you of seeking job security (or rapid insecurity) through inscrutable code, it would be hard to argue. :-) If I were you, I'd put that in a function: =end original あなたの上司が不可解なコードによるジョブセキュリティ(もしくは急激な不安)の 追求を非難していたとしても、説得するのは難しいでしょうね。 :-) もし私があなたの立場だったら、こういった操作は関数に押し込めるでしょう。 =begin original @newAoA = splice_2D( \@AoA, 4 => 8, 7 => 12 ); sub splice_2D { my $lrr = shift; # ref to array of array refs! my ($x_lo, $x_hi, $y_lo, $y_hi) = @_; =end original @newAoA = splice_2D( \@AoA, 4 => 8, 7 => 12 ); sub splice_2D { my $lrr = shift; # リファレンスのリストのリストへのリファレンス! my ($x_lo, $x_hi, $y_lo, $y_hi) = @_; return map { [ @{ $lrr->[$_] } [ $y_lo .. $y_hi ] ] } $x_lo .. $x_hi; } =head1 SEE ALSO L, L, L =head1 AUTHOR Tom Christiansen > Last update: Tue Apr 26 18:30:55 MDT 2011 =begin meta Translate: KIMURA Koichi (5.005_03) Update: SHIRAKATA Kentaro (5.6.1-) Status: completed =end meta