perl参照(一)
8922 ワード
1.一般変数参照variable reference
参照はC言語のポインタのようなもので、参照変数は参照される変数のアドレスを格納します.値を割り当てるときは、変数の前に;を付けることに注意してください.使用するときは$を1つ追加します.
もちろん、参照は単純な変数としてもよいし、参照の参照を用いてもよいし、$を1つ追加することを覚えておいてください.参照は互いに値を割り当てることもできます
D:\>perl reference.plthis is $refv:SCALAR(0x468b20)this is $variable $$refv:this is a reference testthis is reference's reference $$reference :SCALAR(0x468b20)this is $variable $$$refr:this is a reference test
2.配列変数はarray referenceを参照
配列参照は変数参照と同じです
1つの要素$$refa[n]または$refa->[n]を使用
すべての要素を使用:@$refa
結果:
this is @array[0]:thisthis is @array[1]:isthis is @array use @$refa:this is an array reference test
配列使用リファレンスのメリットについては、http://www.cnblogs.com/tobecrazy/archive/2013/06/11/3131887.htmlを参照してください.
3.ハッシュ変数はhash referenceを参照
ハッシュ参照は変数参照配列参照と同様に、コピー時に、使用時に%を加算するだけです.
%$refhハッシュ全体を使用
$$refh{$key}hash要素を使用
実行結果:
this is $$refh:Hashuse whole hash with %$refhc => testa => Hashb => reference
4.匿名引用
a.匿名変数
$refva=\"this is anonymous variable";
使用方法は変数参照と同様に$$refvaのみです
b.匿名配列は角括弧[]を使用することに注意し、使用方法は配列参照と同じである.
$refaa=[qw/this is anonymous array/];
c.匿名ハッシュ注意カッコ{}を使用し、hash参照と同じ方法で使用
$refha{'a'=>"Hash",'b'=>"reference",'c'=>"test" }
まとめ:
1.参照代入にはが必要で、使用時変数は参照変数の前に$を追加し、配列に@ハッシュを追加します.
2.参照は、2つの配列が関数に渡され、配列が1つの配列に圧縮されないようにすることができます.
3.参照は匿名配列変数ハッシュで使用できます
4.リファレンスはperl構造体を作成し、2 D配列を使用します(次のまとめ)
参照はC言語のポインタのようなもので、参照変数は参照される変数のアドレスを格納します.値を割り当てるときは、変数の前に;を付けることに注意してください.使用するときは$を1つ追加します.
もちろん、参照は単純な変数としてもよいし、参照の参照を用いてもよいし、$を1つ追加することを覚えておいてください.参照は互いに値を割り当てることもできます
1 #!/usr/bin/perl -w
2 my $variable="this is a reference test
";
3 my $refv=\$variable;
4 my $refr=\$refv;
5 print "this is \$refv:$refv
";
6 print "this is \$variable \$\$refv:$$refv";
7 print "this is reference's reference \$\$reference :$$refr
";
8 print "this is \$variable \$\$\$refr:$$$refr";
D:\>perl reference.plthis is $refv:SCALAR(0x468b20)this is $variable $$refv:this is a reference testthis is reference's reference $$reference :SCALAR(0x468b20)this is $variable $$$refr:this is a reference test
2.配列変数はarray referenceを参照
配列参照は変数参照と同じです
1 #!/usr/bin/perl -w
2 my @array=qw/this is an array reference test/;
3 my $refa=\@array;
4 print "this is \@array[0]:$refa->[0]
";
5 print "this is \@array[1]:$$refa[1]
";
6 print "this is \@array use \@\$refa:@$refa
";
1つの要素$$refa[n]または$refa->[n]を使用
すべての要素を使用:@$refa
結果:
this is @array[0]:thisthis is @array[1]:isthis is @array use @$refa:this is an array reference test
配列使用リファレンスのメリットについては、http://www.cnblogs.com/tobecrazy/archive/2013/06/11/3131887.htmlを参照してください.
3.ハッシュ変数はhash referenceを参照
ハッシュ参照は変数参照配列参照と同様に、コピー時に、使用時に%を加算するだけです.
1 #!/usr/bin/perl -w
2 my %hash=('a'=>"Hash",'b'=>"reference",'c'=>"test");
3 my $refh=\%hash;
4 print "this is \$\$refh:$$refh{'a'}
";
5 print "use whole hash with \%\$refh
";
6 foreach $key (keys %$refh)
7 {
8 print "$key => $$refh{$key}";
9 print "
";
10 }
%$refhハッシュ全体を使用
$$refh{$key}hash要素を使用
実行結果:
this is $$refh:Hashuse whole hash with %$refhc => testa => Hashb => reference
4.匿名引用
a.匿名変数
$refva=\"this is anonymous variable";
使用方法は変数参照と同様に$$refvaのみです
b.匿名配列は角括弧[]を使用することに注意し、使用方法は配列参照と同じである.
$refaa=[qw/this is anonymous array/];
c.匿名ハッシュ注意カッコ{}を使用し、hash参照と同じ方法で使用
$refha{'a'=>"Hash",'b'=>"reference",'c'=>"test" }
まとめ:
1.参照代入にはが必要で、使用時変数は参照変数の前に$を追加し、配列に@ハッシュを追加します.
2.参照は、2つの配列が関数に渡され、配列が1つの配列に圧縮されないようにすることができます.
3.参照は匿名配列変数ハッシュで使用できます
4.リファレンスはperl構造体を作成し、2 D配列を使用します(次のまとめ)