📄 hashes.mht
字号:
color=3D#cdcd00>%HASH</FONT><FONT color=3D#bebebe>
</FONT><FONT color=3D#ff7f50>delete</FONT><FONT =
color=3D#00ffff>(</FONT><FONT color=3D#cdcd00>$HASH</FONT><FONT =
color=3D#00ffff>{</FONT>$KEY<FONT color=3D#00ffff>});</FONT>
<FONT color=3D#bebebe>#-----------------------------
# </FONT><FONT color=3D#cdcd00>%food_color</FONT><FONT color=3D#bebebe> =
as per Introduction
</FONT><FONT color=3D#ffa500>sub</FONT> <FONT =
color=3D#ff7f50>print</FONT>_foods <FONT color=3D#00ffff>{</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT color=3D#cdad00>@foods</FONT> =
=3D <FONT color=3D#ff7f50>keys</FONT> <FONT =
color=3D#cdcd00>%food_color</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$food</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Keys: =
</FONT><FONT color=3D#cdad00>@foods</FONT><FONT =
color=3D#00cd00>\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Values: =
"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#9ac0cd>$food</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdad00>@foods</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT =
color=3D#9ac0cd>$color</FONT> =3D <FONT =
color=3D#cdcd00>$food_color</FONT><FONT =
color=3D#00ffff>{</FONT>$food<FONT color=3D#00ffff>};</FONT>
<FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#ff7f50>defined</FONT> $color<FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"$color "</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>else</FONT> =
<FONT color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"(undef) "</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#00cd00>"Initially:\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT>_foods<FONT color=3D#00ffff>();</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"\nWith Banana =
undef\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>undef</FONT> <FONT =
color=3D#cdcd00>$food_color</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>"Banana"</FONT><FONT color=3D#00ffff>};</FONT>
<FONT color=3D#ff7f50>print</FONT>_foods<FONT color=3D#00ffff>();</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"\nWith Banana =
deleted\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>delete</FONT> <FONT =
color=3D#cdcd00>$food_color</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>"Banana"</FONT><FONT color=3D#00ffff>};</FONT>
<FONT color=3D#ff7f50>print</FONT>_foods<FONT color=3D#00ffff>();</FONT>
<FONT color=3D#bebebe># Initially:
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Keys: Banana Apple Carrot Lemon
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Values: yellow red orange yellow</FONT><FONT color=3D#43cd80> =
</FONT><FONT color=3D#bebebe>
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# With Banana undef
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Keys: Banana Apple Carrot Lemon
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Values: (undef) red orange yellow</FONT><FONT color=3D#43cd80> =
</FONT><FONT color=3D#bebebe>
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# With Banana deleted
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Keys: Apple Carrot Lemon
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Values: red orange yellow</FONT><FONT color=3D#43cd80> </FONT><FONT =
color=3D#bebebe>
#-----------------------------
</FONT><FONT color=3D#ff7f50>delete</FONT> <FONT =
color=3D#cdcd00>@food_color</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>"Banana"</FONT>, <FONT color=3D#00cd00>"Apple"</FONT>, =
<FONT color=3D#00cd00>"Cabbage"</FONT><FONT color=3D#00ffff>};</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN244>Traversing a Hash</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
<TBODY>
<TR>
<TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT><FONT =
color=3D#00ffff>((</FONT>$key, $value<FONT color=3D#00ffff>)</FONT> =3D =
<FONT color=3D#ff7f50>each</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>%HASH</FONT><FONT color=3D#00ffff>))</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#bebebe># do something with $key and $value
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#9ac0cd>$key</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#ff7f50>keys</FONT> <FONT color=3D#cdcd00>%HASH</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
$value =3D <FONT color=3D#cdcd00>$HASH</FONT><FONT =
color=3D#00ffff>{</FONT>$key<FONT color=3D#00ffff>};</FONT>
<FONT color=3D#bebebe># do something with $key and $value
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
# </FONT><FONT color=3D#cdcd00>%food_color</FONT><FONT color=3D#bebebe> =
per the introduction
</FONT><FONT color=3D#ffa500>while</FONT><FONT =
color=3D#00ffff>((</FONT>$food, $color<FONT color=3D#00ffff>)</FONT> =3D =
<FONT color=3D#ff7f50>each</FONT><FONT color=3D#00ffff>(</FONT><FONT =
color=3D#cdcd00>%food_color</FONT><FONT color=3D#00ffff>))</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"$food is =
$color.\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe># Banana is yellow.
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Apple is red.
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Carrot is orange.
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Lemon is yellow.
</FONT>
<FONT color=3D#ffa500>foreach</FONT> <FONT color=3D#9ac0cd>$food</FONT> =
<FONT color=3D#00ffff>(</FONT><FONT color=3D#ff7f50>keys</FONT> <FONT =
color=3D#cdcd00>%food_color</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ffa500>my</FONT> <FONT color=3D#9ac0cd>$color</FONT> =
=3D <FONT color=3D#cdcd00>$food_color</FONT><FONT =
color=3D#00ffff>{</FONT>$food<FONT color=3D#00ffff>};</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"$food is =
$color.\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe># Banana is yellow.
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Apple is red.
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Carrot is orange.
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Lemon is yellow.
#-----------------------------
</FONT><FONT color=3D#ff7f50>print</FONT>
<FONT color=3D#43cd80> </FONT>
<FONT color=3D#00cd00>"$food
</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#00cd00>
is
</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#00cd00>
</FONT><FONT color=3D#cdcd00>$food_color</FONT><FONT =
color=3D#00cd00>{$food}.\n"</FONT>
<FONT color=3D#43cd80> </FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#9ac0cd>$food</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#ff7f50>sort</FONT> <FONT color=3D#ff7f50>keys</FONT> <FONT =
color=3D#cdcd00>%food_color</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"$food is =
</FONT><FONT color=3D#cdcd00>$food_color</FONT><FONT =
color=3D#00cd00>{$food}.\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe># Apple is red.
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Banana is yellow.
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Carrot is orange.
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Lemon is yellow.
#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT> <FONT color=3D#00ffff>(</FONT> =
<FONT color=3D#00ffff>(</FONT>$k,$v<FONT color=3D#00ffff>)</FONT> =3D =
<FONT color=3D#ff7f50>each</FONT> <FONT =
color=3D#cdcd00>%food_color</FONT> <FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Processing =
$k\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>keys</FONT> <FONT =
color=3D#cdcd00>%food_color</FONT><FONT color=3D#00ffff>;</FONT> =
<FONT color=3D#bebebe># goes back to the start of </FONT><FONT =
color=3D#cdcd00>%food_color</FONT><FONT color=3D#bebebe>
</FONT><FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
# <FONT size=3D-1><A =
href=3D"http://pleac.sourceforge.net/include/perl/ch05/countfrom">downloa=
d the following standalone program</A></FONT>
#!/usr/bin/perl
# countfrom - count number of messages from each sender
</FONT>
$filename =3D <FONT color=3D#cdad00>$ARGV</FONT><FONT =
color=3D#00ffff>[</FONT><FONT color=3D#cdcd00>0</FONT><FONT =
color=3D#00ffff>]</FONT> || <FONT color=3D#00cd00>"-"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#98fb98>open</FONT><FONT color=3D#00ffff>(</FONT>FILE, =
<FONT color=3D#00cd00>"<$filename"</FONT><FONT =
color=3D#00ffff>)</FONT> <FONT color=3D#98fb98>or</FONT> <FONT =
color=3D#ffa500>die</FONT> <FONT color=3D#00cd00>"Can't open $filename : =
$!"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>while</FONT><FONT =
color=3D#00ffff>(</FONT><FILE><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT>/^From: <FONT color=3D#00ffff>(</FONT>.*<FONT =
color=3D#00ffff>)</FONT>/<FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT> <FONT color=3D#cdcd00>$from</FONT><FONT =
color=3D#00ffff>{</FONT>$<FONT color=3D#cdcd00>1</FONT><FONT =
color=3D#00ffff>}</FONT>++ <FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#ffa500>foreach</FONT> <FONT =
color=3D#9ac0cd>$person</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#ff7f50>sort</FONT> <FONT color=3D#ff7f50>keys</FONT> <FONT =
color=3D#cdcd00>%from</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT><FONT color=3D#43cd80> </FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"$person: =
</FONT><FONT color=3D#cdcd00>$from</FONT><FONT =
color=3D#00cd00>{$person}\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN247>Printing a Hash</A></H2>
<TABLE width=3D"100%" bgColor=3D#2f4f4f border=3D0>
<TBODY>
<TR>
<TD><PRE class=3DSCREEN><FONT color=3D#f5deb3 size=3D+1><FONT =
color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ffa500>while</FONT> <FONT color=3D#00ffff>(</FONT> =
<FONT color=3D#00ffff>(</FONT>$k,$v<FONT color=3D#00ffff>)</FONT> =3D =
<FONT color=3D#ff7f50>each</FONT> <FONT color=3D#cdcd00>%hash</FONT> =
<FONT color=3D#00ffff>)</FONT> <FONT color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"$k =3D> =
$v\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT =
color=3D#ff7f50>map</FONT> <FONT color=3D#00ffff>{</FONT> <FONT =
color=3D#00cd00>"$_ =3D> </FONT><FONT =
color=3D#cdcd00>$hash</FONT><FONT color=3D#00cd00>{$_}\n"</FONT> <FONT =
color=3D#00ffff>}</FONT> <FONT color=3D#ff7f50>keys</FONT> <FONT =
color=3D#cdcd00>%hash</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"@{[ =
</FONT><FONT color=3D#cdcd00>%hash</FONT><FONT color=3D#00cd00> =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -