繁体   English   中英

为什么map函数会更改perl中输入数组的值?

[英]why does map function changes the value of the input array in 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

为什么要更改原始数组?

因为您通过修改$_来要求它。

你要

my @reverse_words = map { scalar reverse $_ } @words;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM