510-513.html
来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 128 行
HTML
128 行
<HTML>
<HEAD>
<TITLE>Linux Unleashed, Third Edition:Perl</TITLE>
<SCRIPT>
<!--
function displayWindow(url, width, height) {
var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>
-->
<!--ISBN=0672313723//-->
<!--TITLE=Linux Unleashed, Third Edition//-->
<!--AUTHOR=Tim Parker//-->
<!--PUBLISHER=Macmillan Computer Publishing//-->
<!--IMPRINT=Sams//-->
<!--CHAPTER=28//-->
<!--PAGES=510-513//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="507-510.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="513-516.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<H3><A NAME="Heading9"></A><FONT COLOR="#000077">Arrays</FONT></H3>
<P>An array is an indexed list of data elements. Perl provides two different kinds of arrays. The first kind of array is similar to the arrays that are available in most high-level languages. This kind of array stores single data elements (numbers or strings) into a variable that can store any number of elements. You can get the elements you store in this variable by using the offset value of the element you are trying to find. This means that you can assign a number or a string value to an offset in an array and later use that offset to retrieve that value. For example, the following Perl statement assigns the strings <TT>January</TT>, <TT>1st</TT>, <TT>1999</TT> to the array named <TT>date</TT></P>
<!-- CODE SNIP //-->
<PRE>
@date = (“January”, “1st”, “1999”);
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>The number of elements you can have in the array is limited by the amount of memory you have on your system.<HR></FONT>
</BLOCKQUOTE>
<P>If you later want to retrieve one of the values you have placed into the <TT>date</TT> array, you can do so by stating the array name and the offset of the element you want. When you are retrieving the value of an array element, you must precede the name of the array with a <TT>$</TT> instead of the <TT>@</TT> that you used to define the array. This is because you are now referring to a single array element, which is really the same as a regular variable. For example, to assign the value of <TT>January</TT>, which is the first element in the array, to a variable called <TT>month</TT>, write the following command:</P>
<!-- CODE SNIP //-->
<PRE>
$month=$date[0];
</PRE>
<!-- END CODE SNIP //-->
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>Like many Linux utilities and both C and C++, Perl starts its array indexes at offset <TT>0</TT>. This means that an array of length <TT>5</TT> would have the elements <TT>[0]</TT>, <TT>[1]</TT>, <TT>[2]</TT>, <TT>[3]</TT>, and <TT>[4]</TT>.<HR></FONT>
</BLOCKQUOTE>
<P>The second kind of array that is available in Perl is called an associative array (also called hashes with Perl 5 and later versions. Associative arrays are like normal arrays—they store data elements and have an index. Unlike normal arrays, the index or key is defined by the user and can be a number or a string.
</P>
<P>Because you can define the key values for associative arrays as well as the data associated with each of these key values, assigning and accessing data from an associative array is somewhat different than it is with normal arrays. To assign a value to an associative array, you must also provide the key that you want that value to be associated with. Just as with normal arrays, associative arrays have a special symbol that is used whenever you are performing operations on the entire associative array. You usually assign variables only to elements of an associative array as opposed to dealing with the entire array all at once. The following command assigns the value <TT>hello</TT> to the associative array named <TT>words</TT>, with a key value of <TT>greeting</TT>:</P>
<!-- CODE SNIP //-->
<PRE>
$words{“greeting”} = “hello”;
</PRE>
<!-- END CODE SNIP //-->
<P>An operation that applies to the entire associative array looks like the following:
</P>
<!-- CODE SNIP //-->
<PRE>
%words2 = %words;
</PRE>
<!-- END CODE SNIP //-->
<P>This command assigns the entire contents of the <TT>words</TT> associative array to the <TT>words2</TT> associative array.</P>
<P>To retrieve a value from the <TT>words</TT> associative array, use the following command:</P>
<!-- CODE SNIP //-->
<PRE>
$value=$words{“greeting”};
</PRE>
<!-- END CODE SNIP //-->
<P>This assigns the string <TT>hello</TT> to the <TT>value</TT> variable.</P>
<P>In addition to the assignment operations that can be performed on normal arrays, associative arrays have four more operations: <TT>keys</TT>, <TT>values</TT>, <TT>delete</TT>, and <TT>each</TT>.</P>
<P>The <TT>keys</TT> operator returns a list of all the keys that are defined in the specified associative array. The following commands result in <TT>keyarray</TT> being equal to <TT>(“greeting”, “greeting2”)</TT>.</P>
<!-- CODE SNIP //-->
<PRE>
$words(“greeting”) = “hello”;
$words(“greeting2”) = “good-bye”;
@keyarray = keys(%words);
</PRE>
<!-- END CODE SNIP //-->
<P>The first two commands assign values to individual elements in the associative array <TT>words</TT>. The first command assigns <TT>hello</TT> with a key value of <TT>greeting</TT> to the array, and the second command assigns <TT>good-bye</TT> with a key value of <TT>greeting2</TT> to the array.</P>
<P>The last command assigns all the key values contained in the <TT>words</TT> associative array to the normal array called <TT>keyarray</TT>. Notice the <TT>%</TT> in front of the <TT>words</TT> variable in the <TT>keys</TT> operator. This means that the operation being performed applies to the entire associative array and not just one element of the array.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>Note: </B><BR>The value of <TT>keyarray</TT> that is returned by the preceding commands could actually be <TT>(“greeting2”, “greeting”)</TT>, because values in associative arrays are not stored in any particular order.<HR></FONT>
</BLOCKQUOTE>
<P>Another operator that exists for use with associative arrays is the <TT>values</TT> operator. The <TT>values</TT> operator returns a list of all the values contained in the specified associative array. The following commands result in <TT>valuearray</TT> being equal to (<TT>123.5,105</TT>) or (<TT>105,123.5</TT>).</P>
<!-- CODE SNIP //-->
<PRE>
$cost{“regular”} = 123.5;
$cost(“sale”) = 105;
@valuearray = values(%cost);
</PRE>
<!-- END CODE SNIP //-->
<P>The first two commands again assign values to individual elements in the <TT>cost</TT> associative array. The last command executes the <TT>values</TT> operator on the <TT>cost</TT> associative array and assigns the results to the normal array called <TT>valuearray</TT>.</P>
<P>The <TT>delete</TT> operator enables you to delete elements from an associative array. You call the <TT>delete</TT> operator on a single associative array element, and it deletes both the value of that element and the key value for that element. The following commands illustrate how the <TT>delete</TT> operator works.</P>
<!-- CODE SNIP //-->
<PRE>
%coffee = (“instant”,5.35,”ground”,6.99);
delete $coffee{“ground”};
</PRE>
<!-- END CODE SNIP //-->
<P>The first command assigns two elements to the associative array <TT>coffee</TT>. One of the elements has a key value of <TT>instant</TT> and a value of <TT>5.35</TT>, and the other element has a key value of <TT>ground</TT> and a value of <TT>6.99</TT>. The <TT>delete</TT> operator deletes both the key value and the value of the element in the <TT>coffee</TT> array that has a key value of <TT>ground</TT>.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="507-510.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="513-516.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
</td>
</tr>
</table>
<!-- begin footer information -->
</body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?