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