Last active
August 29, 2015 14:13
-
-
Save tomcha/21613115734244e91483 to your computer and use it in GitHub Desktop.
mapのテスト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use Data::Dumper; | |
use feature ':5.10'; | |
my @a = ('foo.pl', 'bar.pm', 'baz.c', 'plpm.java'); | |
my @aa = ('foo.pl', 'bar.pm', 'baz.c', 'plpm.java'); | |
my @b = map{ $_ .".bak"}@a; #正規表現を使わない直操作map a | |
my @b = map{ $_ = $_ .".bak"}@a; #正規表現を使わない直操作map の代入バージョン b | |
my @c = map{ $_ =~ s/(\.p(l|m)$)/$1.bak/;$_}@aa;#正規表現のmap c | |
my @c = map{ $_ =~ s/(\.p(l|m)$)/$1.bak/;$_ = $_}@aa;#正規表現のmap の代入バージョン d | |
#my @d = grep{ $_ =~ /\.p(l|m)$/}@a; | |
print Dumper \@a; | |
print Dumper \@aa; | |
print Dumper \@b; | |
print Dumper \@c; | |
__END__ | |
# a -> @aは非破壊 | |
# b -> @aは破壊 | |
# c -> @aaは破壊 | |
# d -> @aaは破壊 | |
# $_は元の配列(@aや@aa)の中身を参照する変数なので、その変数の値を更新すれば、 | |
# 当然元の配列の中身も更新される。よって、mapは破壊的関数。ただし、mapのブロ | |
# ック内の式として、$_をあくまで参照するだけの場合(aの場合)は変数の値を更新 | |
# していないので、結果的に元の配列のデータは変更されていない。 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment