perllol.html
来自「perl教程」· HTML 代码 · 共 350 行 · 第 1/3 页
HTML
350 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perllol - Manipulating Arrays of Arrays in Perl</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>perllol - Manipulating Arrays of Arrays in Perl</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#declaration_and_access_of_arrays_of_arrays">Declaration and Access of Arrays of Arrays</a></li>
<li><a href="#growing_your_own">Growing Your Own</a></li>
<li><a href="#access_and_printing">Access and Printing</a></li>
<li><a href="#slices">Slices</a></li>
</ul>
<li><a href="#see_also">SEE ALSO</a></li>
<li><a href="#author">AUTHOR</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perllol - Manipulating Arrays of Arrays in Perl</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>
</p>
<h2><a name="declaration_and_access_of_arrays_of_arrays">Declaration and Access of Arrays of Arrays</a></h2>
<p>The simplest thing to build is an array of arrays (sometimes imprecisely
called a list of lists). It's reasonably easy to understand, and
almost everything that applies here will also be applicable later
on with the fancier data structures.</p>
<p>An array of an array is just a regular old array @AoA that you can
get at with two subscripts, like <code>$AoA[3][2]</code>. Here's a declaration
of the array:</p>
<pre>
<span class="comment"># assign to our array, an array of array references</span>
<span class="variable">@AoA</span> <span class="operator">=</span> <span class="operator">(</span>
<span class="operator">[</span> <span class="string">"fred"</span><span class="operator">,</span> <span class="string">"barney"</span> <span class="operator">]</span><span class="operator">,</span>
<span class="operator">[</span> <span class="string">"george"</span><span class="operator">,</span> <span class="string">"jane"</span><span class="operator">,</span> <span class="string">"elroy"</span> <span class="operator">]</span><span class="operator">,</span>
<span class="operator">[</span> <span class="string">"homer"</span><span class="operator">,</span> <span class="string">"marge"</span><span class="operator">,</span> <span class="string">"bart"</span> <span class="operator">]</span><span class="operator">,</span>
<span class="operator">);</span>
</pre>
<pre>
<span class="keyword">print</span> <span class="variable">$AoA</span><span class="operator">[</span><span class="number">2</span><span class="operator">][</span><span class="number">2</span><span class="operator">]</span><span class="operator">;</span>
<span class="variable">bart</span>
</pre>
<p>Now you should be very careful that the outer bracket type
is a round one, that is, a parenthesis. That's because you're assigning to
an @array, so you need parentheses. If you wanted there <em>not</em> to be an @AoA,
but rather just a reference to it, you could do something more like this:</p>
<pre>
<span class="comment"># assign a reference to array of array references</span>
<span class="variable">$ref_to_AoA</span> <span class="operator">=</span> <span class="operator">[</span>
<span class="operator">[</span> <span class="string">"fred"</span><span class="operator">,</span> <span class="string">"barney"</span><span class="operator">,</span> <span class="string">"pebbles"</span><span class="operator">,</span> <span class="string">"bambam"</span><span class="operator">,</span> <span class="string">"dino"</span><span class="operator">,</span> <span class="operator">]</span><span class="operator">,</span>
<span class="operator">[</span> <span class="string">"homer"</span><span class="operator">,</span> <span class="string">"bart"</span><span class="operator">,</span> <span class="string">"marge"</span><span class="operator">,</span> <span class="string">"maggie"</span><span class="operator">,</span> <span class="operator">]</span><span class="operator">,</span>
<span class="operator">[</span> <span class="string">"george"</span><span class="operator">,</span> <span class="string">"jane"</span><span class="operator">,</span> <span class="string">"elroy"</span><span class="operator">,</span> <span class="string">"judy"</span><span class="operator">,</span> <span class="operator">]</span><span class="operator">,</span>
<span class="operator">]</span><span class="operator">;</span>
</pre>
<pre>
<span class="keyword">print</span> <span class="variable">$ref_to_AoA</span><span class="operator">-></span><span class="operator">[</span><span class="number">2</span><span class="operator">][</span><span class="number">2</span><span class="operator">]</span><span class="operator">;</span>
</pre>
<p>Notice that the outer bracket type has changed, and so our access syntax
has also changed. That's because unlike C, in perl you can't freely
interchange arrays and references thereto. $ref_to_AoA is a reference to an
array, whereas @AoA is an array proper. Likewise, <code>$AoA[2]</code> is not an
array, but an array ref. So how come you can write these:</p>
<pre>
$AoA[2][2]
$ref_to_AoA->[2][2]</pre>
<p>instead of having to write these:</p>
<pre>
$AoA[2]->[2]
$ref_to_AoA->[2]->[2]</pre>
<p>Well, that's because the rule is that on adjacent brackets only (whether
square or curly), you are free to omit the pointer dereferencing arrow.
But you cannot do so for the very first one if it's a scalar containing
a reference, which means that $ref_to_AoA always needs it.</p>
<p>
</p>
<h2><a name="growing_your_own">Growing Your Own</a></h2>
<p>That's all well and good for declaration of a fixed data structure,
but what if you wanted to add new elements on the fly, or build
it up entirely from scratch?</p>
<p>First, let's look at reading it in from a file. This is something like
adding a row at a time. We'll assume that there's a flat file in which
each line is a row and each word an element. If you're trying to develop an
@AoA array containing all these, here's the right way to do that:</p>
<pre>
<span class="keyword">while</span> <span class="operator">(<>)</span> <span class="operator">{</span>
<span class="variable">@tmp</span> <span class="operator">=</span> <span class="keyword">split</span><span class="operator">;</span>
<span class="keyword">push</span> <span class="variable">@AoA</span><span class="operator">,</span> <span class="operator">[</span> <span class="variable">@tmp</span> <span class="operator">]</span><span class="operator">;</span>
<span class="operator">}</span>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?