📄 hashes.mht
字号:
From: <由 Microsoft Internet Explorer 5 保存>
Subject: Hashes
Date: Wed, 29 Apr 2009 09:26:07 +0800
MIME-Version: 1.0
Content-Type: text/html;
charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Content-Location: http://pleac.sourceforge.net/pleac_perl/hashes.html
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5579
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" =
"http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML><HEAD><TITLE>Hashes</TITLE>
<META http-equiv=3DContent-Type content=3D"text/html; charset=3Dgb2312">
<META content=3D"MSHTML 6.00.2900.5659" name=3DGENERATOR><LINK =
title=3DPLEAC-Perl=20
href=3D"index.html" rel=3DHOME><LINK title=3DArrays href=3D"arrays.html" =
rel=3DPREVIOUS><LINK title=3D"Pattern Matching" =
href=3D"patternmatching.html"=20
rel=3DNEXT></HEAD>
<BODY class=3DSECT1 text=3D#cecece vLink=3D#d5ae83 aLink=3D#0000ff =
link=3D#f5deb3=20
bgColor=3D#4f6f6f>
<DIV class=3DNAVHEADER>
<TABLE cellSpacing=3D0 cellPadding=3D0 width=3D"100%"=20
summary=3D"Header navigation table" border=3D0>
<TBODY>
<TR>
<TH align=3Dmiddle colSpan=3D3>PLEAC-Perl</TH></TR>
<TR>
<TD vAlign=3Dbottom align=3Dleft width=3D"10%"><A accessKey=3DP=20
=
href=3D"http://pleac.sourceforge.net/pleac_perl/arrays.html">Prev</A></TD=
>
<TD vAlign=3Dbottom align=3Dmiddle width=3D"80%"></TD>
<TD vAlign=3Dbottom align=3Dright width=3D"10%"><A accessKey=3DN=20
=
href=3D"http://pleac.sourceforge.net/pleac_perl/patternmatching.html">Nex=
t</A></TD></TR></TBODY></TABLE>
<HR align=3Dleft width=3D"100%">
</DIV>
<DIV class=3DSECT1>
<H1 class=3DSECT1><A name=3DHASHES>5. Hashes</A></H1>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN232>Introduction</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#cdcd00>%age</FONT> =3D <FONT =
color=3D#00ffff>(</FONT> <FONT color=3D#00cd00>"Nat"</FONT>, <FONT =
color=3D#cdcd00>24</FONT>,
<FONT color=3D#00cd00>"Jules"</FONT>, <FONT =
color=3D#cdcd00>25</FONT>,
<FONT color=3D#00cd00>"Josh"</FONT>, <FONT =
color=3D#cdcd00>17</FONT> <FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdcd00>$age</FONT><FONT =
color=3D#00ffff>{</FONT><FONT color=3D#00cd00>"Nat"</FONT><FONT =
color=3D#00ffff>}</FONT> =3D <FONT color=3D#cdcd00>24</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#cdcd00>$age</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>"Jules"</FONT><FONT color=3D#00ffff>}</FONT> =3D <FONT =
color=3D#cdcd00>25</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdcd00>$age</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>"Josh"</FONT><FONT color=3D#00ffff>}</FONT> =3D <FONT =
color=3D#cdcd00>17</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdcd00>%food_color</FONT> =3D <FONT =
color=3D#00ffff>(</FONT>
<FONT color=3D#00cd00>"Apple"</FONT> =3D> <FONT =
color=3D#00cd00>"red"</FONT>,
<FONT color=3D#00cd00>"Banana"</FONT> =3D> <FONT =
color=3D#00cd00>"yellow"</FONT>,
<FONT color=3D#00cd00>"Lemon"</FONT> =3D> <FONT =
color=3D#00cd00>"yellow"</FONT>,
<FONT color=3D#00cd00>"Carrot"</FONT> =3D> <FONT =
color=3D#00cd00>"orange"</FONT>
<FONT color=3D#00ffff>);</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT><FONT color=3D#cdcd00>%food_color</FONT> =3D <FONT =
color=3D#00ffff>(</FONT>
<FONT color=3D#00cd00>Apple</FONT> =3D> <FONT =
color=3D#00cd00>"red"</FONT>,
<FONT color=3D#00cd00>Banana</FONT> =3D> <FONT =
color=3D#00cd00>"yellow"</FONT>,
<FONT color=3D#00cd00>Lemon</FONT> =3D> <FONT =
color=3D#00cd00>"yellow"</FONT>,
<FONT color=3D#00cd00>Carrot</FONT> =3D> <FONT =
color=3D#00cd00>"orange"</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=3DAEN235>Adding an Element to 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#cdcd00>$HASH</FONT><FONT =
color=3D#00ffff>{</FONT>$KEY<FONT color=3D#00ffff>}</FONT> =3D =
$VALUE<FONT color=3D#00ffff>;</FONT>
<FONT color=3D#bebebe>#-----------------------------
# </FONT><FONT color=3D#cdcd00>%food_color</FONT><FONT color=3D#bebebe> =
defined per the introduction
</FONT><FONT color=3D#cdcd00>$food_color</FONT><FONT =
color=3D#00ffff>{</FONT><FONT color=3D#00cd00>Raspberry</FONT><FONT =
color=3D#00ffff>}</FONT> =3D <FONT color=3D#00cd00>"pink"</FONT><FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Known =
foods:\n"</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#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\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe># Known foods:
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Banana
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Apple
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Raspberry
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Carrot
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Lemon
#-----------------------------
</FONT></PRE></FONT></TD></TR></TBODY></TABLE></DIV>
<DIV class=3DSECT2>
<H2 class=3DSECT2><A name=3DAEN238>Testing for the Presence of a Key in =
a=20
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>#-----------------------------
# does </FONT><FONT color=3D#cdcd00>%HASH</FONT><FONT color=3D#bebebe> =
have a value for $KEY ?
</FONT><FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#00ffff>(</FONT><FONT color=3D#ff7f50>exists</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#00ffff>{</FONT>
<FONT color=3D#bebebe># it exists
</FONT><FONT color=3D#00ffff>}</FONT> <FONT color=3D#ffa500>else</FONT> =
<FONT color=3D#00ffff>{</FONT>
<FONT color=3D#bebebe># it doesn't
</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>foreach</FONT> <FONT =
color=3D#9ac0cd>$name</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#00cd00>"Banana"</FONT>, <FONT =
color=3D#00cd00>"Martini"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ffa500>if</FONT> <FONT color=3D#00ffff>(</FONT><FONT =
color=3D#ff7f50>exists</FONT> <FONT =
color=3D#cdcd00>$food_color</FONT><FONT =
color=3D#00ffff>{</FONT>$name<FONT color=3D#00ffff>})</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"$name =
is a food.\n"</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>"$name =
is a drink.\n"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe># Banana is a food.
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Martini is a drink.
#-----------------------------
</FONT><FONT color=3D#cdcd00>%age</FONT> =3D <FONT =
color=3D#00ffff>();</FONT>
<FONT color=3D#cdcd00>$age</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>"Toddler"</FONT><FONT color=3D#00ffff>}</FONT> =3D =
<FONT color=3D#cdcd00>3</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdcd00>$age</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>"Unborn"</FONT><FONT color=3D#00ffff>}</FONT> =3D =
<FONT color=3D#cdcd00>0</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#cdcd00>$age</FONT><FONT color=3D#00ffff>{</FONT><FONT =
color=3D#00cd00>"Phantasm"</FONT><FONT color=3D#00ffff>}</FONT> =3D =
<FONT color=3D#ff7f50>undef</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>foreach</FONT> <FONT color=3D#9ac0cd>$thing</FONT> =
<FONT color=3D#00ffff>(</FONT><FONT color=3D#00cd00>"Toddler"</FONT>, =
<FONT color=3D#00cd00>"Unborn"</FONT>, <FONT =
color=3D#00cd00>"Phantasm"</FONT>, <FONT =
color=3D#00cd00>"Relic"</FONT><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"$thing: =
"</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Exists =
"</FONT> <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#ff7f50>exists</FONT> <FONT color=3D#cdcd00>$age</FONT><FONT =
color=3D#00ffff>{</FONT>$thing<FONT color=3D#00ffff>};</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"Defined =
"</FONT> <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#ff7f50>defined</FONT> <FONT color=3D#cdcd00>$age</FONT><FONT =
color=3D#00ffff>{</FONT>$thing<FONT color=3D#00ffff>};</FONT>
<FONT color=3D#ff7f50>print</FONT> <FONT color=3D#00cd00>"True =
"</FONT> <FONT color=3D#ffa500>if</FONT> <FONT =
color=3D#cdcd00>$age</FONT><FONT color=3D#00ffff>{</FONT>$thing<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#bebebe># Toddler: Exists Defined True</FONT><FONT =
color=3D#43cd80> </FONT><FONT color=3D#bebebe>
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Unborn: Exists Defined</FONT><FONT color=3D#43cd80> </FONT><FONT =
color=3D#bebebe>
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Phantasm: Exists</FONT><FONT color=3D#43cd80> </FONT><FONT =
color=3D#bebebe>
#</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
# Relic:</FONT><FONT color=3D#43cd80> </FONT><FONT color=3D#bebebe>
#-----------------------------
</FONT><FONT color=3D#cdcd00>%size</FONT> =3D <FONT =
color=3D#00ffff>();</FONT>
<FONT color=3D#ffa500>while</FONT> <FONT =
color=3D#00ffff>(</FONT><><FONT color=3D#00ffff>)</FONT> <FONT =
color=3D#00ffff>{</FONT>
<FONT color=3D#ff7f50>chomp</FONT><FONT color=3D#00ffff>;</FONT>
<FONT color=3D#ffa500>next</FONT> <FONT color=3D#ffa500>if</FONT> =
<FONT color=3D#cdcd00>$size</FONT><FONT color=3D#00ffff>{</FONT>$_<FONT =
color=3D#00ffff>};</FONT> <FONT color=3D#bebebe># WRONG =
attempt to skip
</FONT> <FONT color=3D#cdcd00>$size</FONT><FONT =
color=3D#00ffff>{</FONT>$_<FONT color=3D#00ffff>}</FONT> =3D <FONT =
color=3D#b2dfee>-</FONT><FONT color=3D#ff7f50>s</FONT> $_<FONT =
color=3D#00ffff>;</FONT>
<FONT color=3D#00ffff>}</FONT>
<FONT color=3D#bebebe>#-----------------------------
</FONT> <FONT color=3D#ffa500>next</FONT> <FONT =
color=3D#ffa500>if</FONT> <FONT color=3D#ff7f50>exists</FONT> <FONT =
color=3D#cdcd00>$size</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=3DAEN241>Deleting from 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>#-----------------------------
# remove $KEY and its value from </FONT><FONT =
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -