⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 00000019.htm

📁 一份很好的linux入门资料
💻 HTM
📖 第 1 页 / 共 5 页
字号:
&nbsp;together.&nbsp;Perl&nbsp;has&nbsp;two&nbsp;types&nbsp;of&nbsp;multivalued&nbsp;variables:&nbsp;arrays&nbsp;and&nbsp;hashes.&nbsp;<BR>&nbsp;In&nbsp;many&nbsp;ways&nbsp;these&nbsp;behave&nbsp;like&nbsp;scalars.&nbsp;They&nbsp;spring&nbsp;into&nbsp;existence&nbsp;with&nbsp;<BR>&nbsp;nothing&nbsp;in&nbsp;them&nbsp;when&nbsp;needed.&nbsp;When&nbsp;you&nbsp;assign&nbsp;to&nbsp;them,&nbsp;they&nbsp;supply&nbsp;a&nbsp;&lt;I&gt;list&lt;/I&gt;&nbsp;<BR>&nbsp;context&nbsp;to&nbsp;the&nbsp;right&nbsp;side&nbsp;of&nbsp;the&nbsp;assignment.&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;P&gt;You'd&nbsp;use&nbsp;an&nbsp;array&nbsp;when&nbsp;you&nbsp;want&nbsp;to&nbsp;look&nbsp;something&nbsp;up&nbsp;by&nbsp;number.&nbsp;You'd&nbsp;<BR>&nbsp;use&nbsp;a&nbsp;hash&nbsp;when&nbsp;you&nbsp;want&nbsp;to&nbsp;look&nbsp;something&nbsp;up&nbsp;by&nbsp;name.&nbsp;The&nbsp;two&nbsp;concepts&nbsp;<BR>&nbsp;are&nbsp;complementary.&nbsp;You'll&nbsp;often&nbsp;see&nbsp;people&nbsp;using&nbsp;an&nbsp;array&nbsp;to&nbsp;translate&nbsp;<BR>&nbsp;month&nbsp;numbers&nbsp;into&nbsp;month&nbsp;names,&nbsp;and&nbsp;a&nbsp;corresponding&nbsp;hash&nbsp;to&nbsp;translate&nbsp;month&nbsp;<BR>&nbsp;names&nbsp;back&nbsp;into&nbsp;month&nbsp;numbers.&nbsp;(Though&nbsp;hashes&nbsp;aren't&nbsp;limited&nbsp;to&nbsp;holding&nbsp;<BR>&nbsp;only&nbsp;numbers.&nbsp;You&nbsp;could&nbsp;have&nbsp;a&nbsp;hash&nbsp;that&nbsp;translates&nbsp;month&nbsp;names&nbsp;to&nbsp;birthstone&nbsp;<BR>&nbsp;names,&nbsp;for&nbsp;instance.)&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;H5&gt;&lt;A&nbsp;NAME=&quot;PERL2-CH-1-SECT-2.1.2.1&quot;&gt;&lt;/A&gt;Arrays.&lt;/H5&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;P&gt;An&nbsp;&lt;I&gt;array&lt;/I&gt;&nbsp;is&nbsp;an&nbsp;ordered&nbsp;list&nbsp;of&nbsp;scalars,&nbsp;accessed[9]&nbsp;by&nbsp;the&nbsp;scalar's&nbsp;<BR>&nbsp;position&nbsp;in&nbsp;the&nbsp;list.&nbsp;The&nbsp;list&nbsp;may&nbsp;contain&nbsp;numbers,&nbsp;or&nbsp;strings,&nbsp;or&nbsp;a&nbsp;mixture&nbsp;<BR>&nbsp;of&nbsp;both.&nbsp;(In&nbsp;fact,&nbsp;it&nbsp;could&nbsp;also&nbsp;contain&nbsp;references&nbsp;to&nbsp;other&nbsp;lists,&nbsp;but&nbsp;<BR>&nbsp;we'll&nbsp;get&nbsp;to&nbsp;that&nbsp;in&nbsp;&lt;A&nbsp;HREF=&quot;ch04_01.htm&quot;&gt;Chapter&nbsp;4,&nbsp;&lt;I&gt;References&nbsp;and&nbsp;<BR>&nbsp;Nested&nbsp;Data&nbsp;Structures&lt;/I&gt;&lt;/A&gt;,&nbsp;when&nbsp;we're&nbsp;discussing&nbsp;multidimensional&nbsp;<BR>&nbsp;arrays.)&nbsp;To&nbsp;assign&nbsp;a&nbsp;list&nbsp;value&nbsp;to&nbsp;an&nbsp;array,&nbsp;you&nbsp;simply&nbsp;group&nbsp;the&nbsp;variables&nbsp;<BR>&nbsp;together&nbsp;(with&nbsp;a&nbsp;set&nbsp;of&nbsp;parentheses):&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;BLOCKQUOTE&nbsp;class=footnote&gt;&nbsp;<BR>&nbsp;&lt;P&gt;[9]&nbsp;Or&nbsp;keyed,&nbsp;or&nbsp;indexed,&nbsp;or&nbsp;subscripted,&nbsp;or&nbsp;looked&nbsp;up.&nbsp;Take&nbsp;your&nbsp;pick.&nbsp;<BR>&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&lt;/BLOCKQUOTE&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;PRE&gt;@home&nbsp;=&nbsp;(&amp;quot;couch&amp;quot;,&nbsp;&amp;quot;chair&amp;quot;,&nbsp;&amp;quot;table&amp;quot;,&nbsp;&amp;quot;stove&amp;quot;);&nbsp;<BR>&nbsp;&lt;/PRE&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;P&gt;Conversely,&nbsp;if&nbsp;you&nbsp;use&nbsp;&lt;TT&gt;@home&lt;/TT&gt;&nbsp;in&nbsp;a&nbsp;list&nbsp;context,&nbsp;such&nbsp;as&nbsp;on&nbsp;<BR>&nbsp;the&nbsp;right&nbsp;side&nbsp;of&nbsp;a&nbsp;list&nbsp;assignment,&nbsp;you&nbsp;get&nbsp;back&nbsp;out&nbsp;the&nbsp;same&nbsp;list&nbsp;you&nbsp;<BR>&nbsp;put&nbsp;in.&nbsp;So&nbsp;you&nbsp;could&nbsp;set&nbsp;four&nbsp;scalar&nbsp;variables&nbsp;from&nbsp;the&nbsp;array&nbsp;like&nbsp;this:&nbsp;<BR>&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;PRE&gt;($potato,&nbsp;$lift,&nbsp;$tennis,&nbsp;$pipe)&nbsp;=&nbsp;@home;&nbsp;<BR>&nbsp;&lt;/PRE&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;P&gt;These&nbsp;are&nbsp;called&nbsp;list&nbsp;assignments.&nbsp;They&nbsp;logically&nbsp;happen&nbsp;in&nbsp;parallel,&nbsp;<BR>&nbsp;so&nbsp;you&nbsp;can&nbsp;swap&nbsp;two&nbsp;variables&nbsp;by&nbsp;saying:&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;PRE&gt;($alpha,$omega)&nbsp;=&nbsp;($omega,$alpha);&nbsp;<BR>&nbsp;&lt;/PRE&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;P&gt;As&nbsp;in&nbsp;C,&nbsp;arrays&nbsp;are&nbsp;zero-based,&nbsp;so&nbsp;while&nbsp;you&nbsp;would&nbsp;talk&nbsp;about&nbsp;the&nbsp;first&nbsp;<BR>&nbsp;through&nbsp;fourth&nbsp;elements&nbsp;of&nbsp;the&nbsp;array,&nbsp;you&nbsp;would&nbsp;get&nbsp;to&nbsp;them&nbsp;with&nbsp;subscripts&nbsp;<BR>&nbsp;0&nbsp;through&nbsp;3.[10]&nbsp;Array&nbsp;subscripts&nbsp;are&nbsp;enclosed&nbsp;in&nbsp;square&nbsp;brackets&nbsp;[like&nbsp;<BR>&nbsp;this],&nbsp;so&nbsp;if&nbsp;you&nbsp;want&nbsp;to&nbsp;select&nbsp;an&nbsp;individual&nbsp;array&nbsp;element,&nbsp;you&nbsp;would&nbsp;<BR>&nbsp;refer&nbsp;to&nbsp;it&nbsp;as&nbsp;&lt;TT&gt;$home[&lt;/TT&gt;&lt;I&gt;n&lt;/I&gt;&lt;TT&gt;]&lt;/TT&gt;,&nbsp;where&nbsp;&lt;I&gt;n&lt;/I&gt;&nbsp;is&nbsp;the&nbsp;<BR>&nbsp;subscript&nbsp;(one&nbsp;less&nbsp;than&nbsp;the&nbsp;element&nbsp;number)&nbsp;you&nbsp;want.&nbsp;See&nbsp;the&nbsp;example&nbsp;<BR>&nbsp;below.&nbsp;Since&nbsp;the&nbsp;element&nbsp;you&nbsp;are&nbsp;dealing&nbsp;with&nbsp;is&nbsp;a&nbsp;scalar,&nbsp;you&nbsp;always&nbsp;precede&nbsp;<BR>&nbsp;it&nbsp;with&nbsp;a&nbsp;&lt;TT&gt;$&lt;/TT&gt;.&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;BLOCKQUOTE&nbsp;class=footnote&gt;&nbsp;<BR>&nbsp;&lt;P&gt;[10]&nbsp;If&nbsp;this&nbsp;seems&nbsp;odd&nbsp;to&nbsp;you,&nbsp;just&nbsp;think&nbsp;of&nbsp;the&nbsp;subscript&nbsp;as&nbsp;an&nbsp;offset,&nbsp;<BR>&nbsp;that&nbsp;is,&nbsp;the&nbsp;count&nbsp;of&nbsp;how&nbsp;many&nbsp;array&nbsp;elements&nbsp;come&nbsp;before&nbsp;it.&nbsp;Obviously,&nbsp;<BR>&nbsp;the&nbsp;first&nbsp;element&nbsp;doesn't&nbsp;have&nbsp;any&nbsp;elements&nbsp;before&nbsp;it,&nbsp;and&nbsp;so&nbsp;has&nbsp;an&nbsp;offset&nbsp;<BR>&nbsp;of&nbsp;0.&nbsp;This&nbsp;is&nbsp;how&nbsp;computers&nbsp;think.&nbsp;(We&nbsp;think.)&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&lt;/BLOCKQUOTE&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;P&gt;If&nbsp;you&nbsp;want&nbsp;to&nbsp;assign&nbsp;to&nbsp;one&nbsp;array&nbsp;element&nbsp;at&nbsp;a&nbsp;time,&nbsp;you&nbsp;could&nbsp;write&nbsp;<BR>&nbsp;the&nbsp;earlier&nbsp;assignment&nbsp;as:&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;PRE&gt;$home[0]&nbsp;=&nbsp;&amp;quot;couch&amp;quot;;&nbsp;<BR>&nbsp;$home[1]&nbsp;=&nbsp;&amp;quot;chair&amp;quot;;&nbsp;<BR>&nbsp;$home[2]&nbsp;=&nbsp;&amp;quot;table&amp;quot;;&nbsp;<BR>&nbsp;$home[3]&nbsp;=&nbsp;&amp;quot;stove&amp;quot;;&nbsp;<BR>&nbsp;&lt;/PRE&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;P&gt;Since&nbsp;arrays&nbsp;are&nbsp;ordered,&nbsp;there&nbsp;are&nbsp;various&nbsp;useful&nbsp;operations&nbsp;that&nbsp;you&nbsp;<BR>&nbsp;can&nbsp;do&nbsp;on&nbsp;them,&nbsp;such&nbsp;as&nbsp;the&nbsp;stack&nbsp;operations,&nbsp;&lt;A&nbsp;HREF=&quot;ch03_02.htm#PERL2-CMD-PUSH&quot;&gt;push&lt;/A&gt;&nbsp;<BR>&nbsp;and&nbsp;&lt;A&nbsp;HREF=&quot;ch03_02.htm#PERL2-CMD-POP&quot;&gt;pop&lt;/A&gt;.&nbsp;A&nbsp;stack&nbsp;is,&nbsp;after&nbsp;all,&nbsp;<BR>&nbsp;just&nbsp;an&nbsp;ordered&nbsp;list,&nbsp;with&nbsp;a&nbsp;beginning&nbsp;and&nbsp;an&nbsp;end.&nbsp;Especially&nbsp;an&nbsp;end.&nbsp;Perl&nbsp;<BR>&nbsp;regards&nbsp;the&nbsp;end&nbsp;of&nbsp;your&nbsp;list&nbsp;as&nbsp;the&nbsp;top&nbsp;of&nbsp;a&nbsp;stack.&nbsp;(Although&nbsp;most&nbsp;Perl&nbsp;<BR>&nbsp;programmers&nbsp;think&nbsp;of&nbsp;a&nbsp;list&nbsp;as&nbsp;horizontal,&nbsp;with&nbsp;the&nbsp;top&nbsp;of&nbsp;the&nbsp;stack&nbsp;on&nbsp;<BR>&nbsp;the&nbsp;right.)&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;H5&gt;&lt;A&nbsp;NAME=&quot;PERL2-CH-1-SECT-2.1.2.2&quot;&gt;&lt;/A&gt;Hashes.&lt;/H5&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;P&gt;A&nbsp;&lt;I&gt;hash&lt;/I&gt;&nbsp;is&nbsp;an&nbsp;unordered&nbsp;set&nbsp;of&nbsp;scalars,&nbsp;accessed[11]&nbsp;by&nbsp;some&nbsp;string&nbsp;<BR>&nbsp;value&nbsp;that&nbsp;is&nbsp;associated&nbsp;with&nbsp;each&nbsp;scalar.&nbsp;For&nbsp;this&nbsp;reason&nbsp;hashes&nbsp;are&nbsp;often&nbsp;<BR>&nbsp;called&nbsp;&amp;quot;associative&nbsp;arrays&amp;quot;.&nbsp;But&nbsp;that's&nbsp;too&nbsp;long&nbsp;for&nbsp;lazy&nbsp;typists&nbsp;<BR>&nbsp;to&nbsp;type,&nbsp;and&nbsp;we&nbsp;talk&nbsp;about&nbsp;them&nbsp;so&nbsp;often&nbsp;that&nbsp;we&nbsp;decided&nbsp;to&nbsp;name&nbsp;them&nbsp;something&nbsp;<BR>&nbsp;short&nbsp;and&nbsp;snappy.[12]&nbsp;The&nbsp;other&nbsp;reason&nbsp;we&nbsp;picked&nbsp;the&nbsp;name&nbsp;&amp;quot;hash&amp;quot;&nbsp;<BR>&nbsp;is&nbsp;to&nbsp;emphasize&nbsp;the&nbsp;fact&nbsp;that&nbsp;they're&nbsp;disordered.&nbsp;(They&nbsp;are,&nbsp;coincidentally,&nbsp;<BR>&nbsp;implemented&nbsp;internally&nbsp;using&nbsp;a&nbsp;hash-table&nbsp;lookup,&nbsp;which&nbsp;is&nbsp;why&nbsp;hashes&nbsp;are&nbsp;<BR>&nbsp;so&nbsp;fast,&nbsp;and&nbsp;stay&nbsp;so&nbsp;fast&nbsp;no&nbsp;matter&nbsp;how&nbsp;many&nbsp;values&nbsp;you&nbsp;put&nbsp;into&nbsp;them.)&nbsp;<BR>&nbsp;You&nbsp;can't&nbsp;&lt;A&nbsp;HREF=&quot;ch03_02.htm#PERL2-CMD-PUSH&quot;&gt;push&lt;/A&gt;&nbsp;or&nbsp;&lt;A&nbsp;HREF=&quot;ch03_02.htm#PERL2-CMD-POP&quot;&gt;pop&lt;/A&gt;&nbsp;<BR>&nbsp;a&nbsp;hash&nbsp;though,&nbsp;because&nbsp;it&nbsp;doesn't&nbsp;make&nbsp;sense.&nbsp;A&nbsp;hash&nbsp;has&nbsp;no&nbsp;beginning&nbsp;or&nbsp;<BR>&nbsp;end.&nbsp;Nevertheless,&nbsp;hashes&nbsp;are&nbsp;extremely&nbsp;powerful&nbsp;and&nbsp;useful.&nbsp;Until&nbsp;you&nbsp;<BR>&nbsp;start&nbsp;thinking&nbsp;in&nbsp;terms&nbsp;of&nbsp;hashes,&nbsp;you&nbsp;aren't&nbsp;really&nbsp;thinking&nbsp;in&nbsp;Perl.&nbsp;<BR>&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;o&nbsp;find&nbsp;your&nbsp;script.&nbsp;Something&nbsp;like&nbsp;<BR>&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;BLOCKQUOTE&nbsp;class=footnote&gt;&nbsp;<BR>&nbsp;&lt;P&gt;[16]&nbsp;Although&nbsp;Perl&nbsp;has&nbsp;its&nbsp;share&nbsp;of&nbsp;funny&nbsp;notations,&nbsp;this&nbsp;one&nbsp;must&nbsp;be&nbsp;<BR>&nbsp;blamed&nbsp;on&nbsp;UNIX.&nbsp;&lt;I&gt;chmod&lt;/I&gt;&nbsp;(1)&nbsp;means&nbsp;you&nbsp;should&nbsp;refer&nbsp;to&nbsp;the&nbsp;manpage&nbsp;<BR>&nbsp;for&nbsp;the&nbsp;&lt;I&gt;chmod&lt;/I&gt;&nbsp;command&nbsp;in&nbsp;section&nbsp;one&nbsp;of&nbsp;your&nbsp;UNIX&nbsp;manual.&nbsp;If&nbsp;you&nbsp;<BR>&nbsp;type&nbsp;either&nbsp;&lt;TT&gt;man&nbsp;1&nbsp;chmod&lt;/TT&gt;&nbsp;or&nbsp;&lt;TT&gt;man&nbsp;-s&nbsp;1&nbsp;chmod&lt;/TT&gt;&nbsp;(depending&nbsp;<BR>&nbsp;on&nbsp;your&nbsp;flavor&nbsp;of&nbsp;UNIX),&nbsp;you&nbsp;should&nbsp;be&nbsp;able&nbsp;to&nbsp;find&nbsp;out&nbsp;all&nbsp;the&nbsp;interesting&nbsp;<BR>&nbsp;information&nbsp;your&nbsp;system&nbsp;knows&nbsp;about&nbsp;the&nbsp;command&nbsp;&lt;I&gt;chmod&lt;/I&gt;.&nbsp;(Of&nbsp;course,&nbsp;<BR>&nbsp;if&nbsp;your&nbsp;flavor&nbsp;of&nbsp;UNIX&nbsp;happens&nbsp;to&nbsp;be&nbsp;&amp;quot;Not&nbsp;UNIX!&amp;quot;&nbsp;then&nbsp;you'll&nbsp;<BR>&nbsp;need&nbsp;to&nbsp;refer&nbsp;to&nbsp;your&nbsp;system's&nbsp;documentation&nbsp;for&nbsp;the&nbsp;equivalent&nbsp;command,&nbsp;<BR>&nbsp;presuming&nbsp;you&nbsp;are&nbsp;so&nbsp;blessed.&nbsp;Your&nbsp;chief&nbsp;consolation&nbsp;is&nbsp;that,&nbsp;if&nbsp;an&nbsp;equivalent&nbsp;<BR>&nbsp;command&nbsp;does&nbsp;exist,&nbsp;it&nbsp;will&nbsp;have&nbsp;a&nbsp;much&nbsp;better&nbsp;name&nbsp;than&nbsp;&lt;I&gt;chmod&lt;/I&gt;.)&nbsp;<BR>&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&lt;/BLOCKQUOTE&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;PRE&gt;%&nbsp;../bin/gradation&nbsp;<BR>&nbsp;&lt;/PRE&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;P&gt;Finally,&nbsp;if&nbsp;you&nbsp;are&nbsp;unfortunate&nbsp;enough&nbsp;to&nbsp;be&nbsp;on&nbsp;an&nbsp;ancient&nbsp;UNIX&nbsp;system&nbsp;<BR>&nbsp;that&nbsp;doesn't&nbsp;support&nbsp;the&nbsp;magic&nbsp;&lt;TT&gt;#!&lt;/TT&gt;&nbsp;line,&nbsp;or&nbsp;if&nbsp;the&nbsp;path&nbsp;to&nbsp;your&nbsp;<BR>&nbsp;interpreter&nbsp;is&nbsp;longer&nbsp;than&nbsp;32&nbsp;characters&nbsp;(a&nbsp;built-in&nbsp;limit&nbsp;on&nbsp;many&nbsp;systems),&nbsp;<BR>&nbsp;you&nbsp;may&nbsp;be&nbsp;able&nbsp;to&nbsp;work&nbsp;around&nbsp;it&nbsp;like&nbsp;this:&nbsp;&lt;/P&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;PRE&gt;#!/bin/sh&nbsp;--&nbsp;#&nbsp;perl,&nbsp;to&nbsp;stop&nbsp;looping&nbsp;<BR>&nbsp;eval&nbsp;'exec&nbsp;/usr/bin/perl&nbsp;-S&nbsp;$0&nbsp;${1+&amp;quot;$@&amp;quot;}'&nbsp;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;0;&nbsp;<BR>&nbsp;&lt;/PRE&gt;&nbsp;<BR>&nbsp;&nbsp;<BR>&nbsp;&lt;P&gt;Some&nbsp;operating&nbsp;systems&nbsp;may&nbsp;require&nbsp;variants&nbsp;on&nbsp;this&nbsp;to&nbsp;deal&nbsp;with&nbsp;&lt;I&gt;/bin/csh&lt;/I&gt;,&nbsp;<BR>&nbsp;&lt;I&gt;DCL&lt;/I&gt;,&nbsp;&lt;I&gt;COMMAND.COM&lt;/I&gt;,&nbsp;or&nbsp;whatever&nbsp;happens&nbsp;to&nbsp;be&nbsp;your&nbsp;default&nbsp;<BR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -