为什么map函数会改变perl中输入数组的值? 为了说明,
#!/usr/bin/env perl
use strict;
use warnings;
use v5.10;
my @words = <DATA>;
# want to have another array that contains the each word in reverse order
my @reverse_words = map { $_ = scalar reverse $_ } @words;
say $words[0]; # want to check the content of first element of original array
say $reverse_words[0]; # new
__DATA__
aarhus
aaron
ababa
aback
但这打印
$perl findPalindrome.pl
suhraa
suhraa
为什么原始数组会改变?
答案 0 :(得分:4)
因为您通过修改$_
来询问它。
你想要
my @reverse_words = map { scalar reverse $_ } @words;