Perl Map方法

-4 perl map

需要知道这一点,

Open File;
map{ chomp; $isword{uc join "", sort /./g}.= "$_+" } <File>;
Run Code Online (Sandbox Code Playgroud)

想知道这张地图的作用和它会返回什么..无法理解排序和为什么uc加入"".

用于此程序的文件包含wordlist.

有人请帮忙..

ike*_*ami 5

它返回的是没有实际意义的,因为它被丢弃了.这使得它成为一种奇怪的用法map.人们通常会写

for (<File>) { chomp; $isword{uc join "", sort /./g}.= "$_+"; }
Run Code Online (Sandbox Code Playgroud)

与之相比,这是浪费记忆

while (<File>) { chomp; $isword{uc join "", sort /./g} .= "$_+"; }
Run Code Online (Sandbox Code Playgroud)

对于它遇到的每一行,它会将形成该行的字符标准化.

apple becomes AELPP
orange becomes AEGNOR
art becomes ART
rat becomes ART
tar becomes ART
etc
Run Code Online (Sandbox Code Playgroud)

它使用该规范化形式作为哈希的关键,并将该行存储在该键上.

$isword{AELPP} = 'apple+';
$isword{AEGNOR} = 'orange+';
$isword{ART} = 'art+rat+tar+';
Run Code Online (Sandbox Code Playgroud)

假设每一行都是一个单词,它允许人们快速查找单词的字谜.

sub find_anagrams { split /\+/, $isword{uc join "", sort /./g} // '' }
Run Code Online (Sandbox Code Playgroud)