perldsc.html

来自「perl教程」· HTML 代码 · 共 743 行 · 第 1/5 页

HTML
743 行
字号
<?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>perldsc - Perl Data Structures Cookbook</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>perldsc - Perl Data Structures Cookbook</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>
	<li><a href="#references">REFERENCES</a></li>
	<li><a href="#common_mistakes">COMMON MISTAKES</a></li>
	<li><a href="#caveat_on_precedence">CAVEAT ON PRECEDENCE</a></li>
	<li><a href="#why_you_should_always_use_strict">WHY YOU SHOULD ALWAYS <code>use strict</code></a></li>
	<li><a href="#debugging">DEBUGGING</a></li>
	<li><a href="#code_examples">CODE EXAMPLES</a></li>
	<li><a href="#arrays_of_arrays">ARRAYS OF ARRAYS</a></li>
	<ul>

		<li><a href="#declaration_of_an_array_of_arrays">Declaration of an ARRAY OF ARRAYS</a></li>
		<li><a href="#generation_of_an_array_of_arrays">Generation of an ARRAY OF ARRAYS</a></li>
		<li><a href="#access_and_printing_of_an_array_of_arrays">Access and Printing of an ARRAY OF ARRAYS</a></li>
	</ul>

	<li><a href="#hashes_of_arrays">HASHES OF ARRAYS</a></li>
	<ul>

		<li><a href="#declaration_of_a_hash_of_arrays">Declaration of a HASH OF ARRAYS</a></li>
		<li><a href="#generation_of_a_hash_of_arrays">Generation of a HASH OF ARRAYS</a></li>
		<li><a href="#access_and_printing_of_a_hash_of_arrays">Access and Printing of a HASH OF ARRAYS</a></li>
	</ul>

	<li><a href="#arrays_of_hashes">ARRAYS OF HASHES</a></li>
	<ul>

		<li><a href="#declaration_of_an_array_of_hashes">Declaration of an ARRAY OF HASHES</a></li>
		<li><a href="#generation_of_an_array_of_hashes">Generation of an ARRAY OF HASHES</a></li>
		<li><a href="#access_and_printing_of_an_array_of_hashes">Access and Printing of an ARRAY OF HASHES</a></li>
	</ul>

	<li><a href="#hashes_of_hashes">HASHES OF HASHES</a></li>
	<ul>

		<li><a href="#declaration_of_a_hash_of_hashes">Declaration of a HASH OF HASHES</a></li>
		<li><a href="#generation_of_a_hash_of_hashes">Generation of a HASH OF HASHES</a></li>
		<li><a href="#access_and_printing_of_a_hash_of_hashes">Access and Printing of a HASH OF HASHES</a></li>
	</ul>

	<li><a href="#more_elaborate_records">MORE ELABORATE RECORDS</a></li>
	<ul>

		<li><a href="#declaration_of_more_elaborate_records">Declaration of MORE ELABORATE RECORDS</a></li>
		<li><a href="#declaration_of_a_hash_of_complex_records">Declaration of a HASH OF COMPLEX RECORDS</a></li>
		<li><a href="#generation_of_a_hash_of_complex_records">Generation of a HASH OF COMPLEX RECORDS</a></li>
	</ul>

	<li><a href="#database_ties">Database Ties</a></li>
	<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>perldsc - Perl Data Structures Cookbook</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>The single feature most sorely lacking in the Perl programming language
prior to its 5.0 release was complex data structures.  Even without direct
language support, some valiant programmers did manage to emulate them, but
it was hard work and not for the faint of heart.  You could occasionally
get away with the <code>$m{$AoA,$b}</code> notation borrowed from <strong>awk</strong> in which the
keys are actually more like a single concatenated string <code>&quot;$AoA$b&quot;</code>, but
traversal and sorting were difficult.  More desperate programmers even
hacked Perl's internal symbol table directly, a strategy that proved hard
to develop and maintain--to put it mildly.</p>
<p>The 5.0 release of Perl let us have complex data structures.  You
may now write something like this and all of a sudden, you'd have an array
with three dimensions!</p>
<pre>
    <span class="keyword">for</span> <span class="variable">$x</span> <span class="operator">(</span><span class="number">1</span> <span class="operator">..</span> <span class="number">10</span><span class="operator">)</span> <span class="operator">{</span>
        <span class="keyword">for</span> <span class="variable">$y</span> <span class="operator">(</span><span class="number">1</span> <span class="operator">..</span> <span class="number">10</span><span class="operator">)</span> <span class="operator">{</span>
            <span class="keyword">for</span> <span class="variable">$z</span> <span class="operator">(</span><span class="number">1</span> <span class="operator">..</span> <span class="number">10</span><span class="operator">)</span> <span class="operator">{</span>
                <span class="variable">$AoA</span><span class="operator">[</span><span class="variable">$x</span><span class="operator">][</span><span class="variable">$y</span><span class="operator">][</span><span class="variable">$z</span><span class="operator">]</span> <span class="operator">=</span>
                    <span class="variable">$x</span> <span class="operator">**</span> <span class="variable">$y</span> <span class="operator">+</span> <span class="variable">$z</span><span class="operator">;</span>
            <span class="operator">}</span>
        <span class="operator">}</span>
    <span class="operator">}</span>
</pre>
<p>Alas, however simple this may appear, underneath it's a much more
elaborate construct than meets the eye!</p>
<p>How do you print it out?  Why can't you say just <code>print @AoA</code>?  How do
you sort it?  How can you pass it to a function or get one of these back
from a function?  Is it an object?  Can you save it to disk to read
back later?  How do you access whole rows or columns of that matrix?  Do
all the values have to be numeric?</p>
<p>As you see, it's quite easy to become confused.  While some small portion
of the blame for this can be attributed to the reference-based
implementation, it's really more due to a lack of existing documentation with
examples designed for the beginner.</p>
<p>This document is meant to be a detailed but understandable treatment of the
many different sorts of data structures you might want to develop.  It
should also serve as a cookbook of examples.  That way, when you need to
create one of these complex data structures, you can just pinch, pilfer, or
purloin a drop-in example from here.</p>
<p>Let's look at each of these possible constructs in detail.  There are separate
sections on each of the following:</p>
<ul>
<li><strong><a name="item_arrays_of_arrays">arrays of arrays</a></strong>

<li><strong><a name="item_hashes_of_arrays">hashes of arrays</a></strong>

<li><strong><a name="item_arrays_of_hashes">arrays of hashes</a></strong>

<li><strong><a name="item_hashes_of_hashes">hashes of hashes</a></strong>

<li><strong><a name="item_more_elaborate_constructs">more elaborate constructs</a></strong>

</ul>
<p>But for now, let's look at general issues common to all
these types of data structures.</p>
<p>
</p>
<hr />
<h1><a name="references">REFERENCES</a></h1>
<p>The most important thing to understand about all data structures in Perl
-- including multidimensional arrays--is that even though they might
appear otherwise, Perl <code>@ARRAY</code>s and <code>%HASH</code>es are all internally

⌨️ 快捷键说明

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