1
为什么map函数在perl中改变输入数组的值? 为了说明,为什么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
为什么原数组改变?
这样你就不会用数组元素的复制版本处理?这不危险吗? – Alby
我不知道这意味着什么 – ikegami
对不起。我的意思是这样的:$ _是指向单词中每个元素的指针,而不是复制的元素?所以通过$ _ =标量反转$ _,你正在有效地改变单词中的元素,是否正确? – Alby