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