CINXE.COM

perllol - Manipulating Arrays of Arrays in Perl - Perldoc Browser

<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>perllol - Manipulating Arrays of Arrays in Perl - Perldoc Browser</title> <link rel="search" href="/opensearch.xml" type="application/opensearchdescription+xml" title="Perldoc Browser"> <link rel="canonical" href="https://perldoc.perl.org/perllol"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.5.0/styles/stackoverflow-light.min.css" integrity="sha512-cG1IdFxqipi3gqLmksLtuk13C+hBa57a6zpWxMeoY3Q9O6ooFxq50DayCdm0QrDgZjMUn23z/0PMZlgft7Yp5Q==" crossorigin="anonymous" /> <style> body { background: #f4f4f5; color: #020202; } .navbar-dark { background-image: -webkit-linear-gradient(top, #005f85 0, #002e49 100%); background-image: -o-linear-gradient(top, #005f85 0, #002e49 100%); background-image: linear-gradient(to bottom, #005f85 0, #002e49 100%); filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ff005f85', endColorstr='#ff002e49', GradientType=0); background-repeat: repeat-x; } .navbar-dark .navbar-nav .nav-link, .navbar-dark .navbar-nav .nav-link:focus { color: #fff } .navbar-dark .navbar-nav .nav-link:hover { color: #ffef68 } #wrapperlicious { margin: 0 auto; font: 0.9em 'Helvetica Neue', Helvetica, sans-serif; font-weight: normal; line-height: 1.5em; margin: 0; padding: 0; } #wrapperlicious h1 { font-size: 1.5em } #wrapperlicious h2 { font-size: 1.3em } #wrapperlicious h3 { font-size: 1.1em } #wrapperlicious h4 { font-size: 0.9em } #wrapperlicious h1, #wrapperlicious h2, #wrapperlicious h3, #wrapperlicious h4, #wrapperlicious dt { color: #020202; margin-top: 1em; margin-bottom: 1em; position: relative; font-weight: bold; } #wrapperlicious a { color: inherit; text-decoration: underline } #wrapperlicious #toc { text-decoration: none } #wrapperlicious a:hover { color: #2a2a2a } #wrapperlicious a img { border: 0 } #wrapperlicious :not(pre) > code { color: inherit; background-color: rgba(0, 0, 0, 0.04); border-radius: 3px; font: 0.9em Consolas, Menlo, Monaco, monospace; padding: 0.3em; } #wrapperlicious dd { margin: 0; margin-left: 2em; } #wrapperlicious dt { color: #2a2a2a; font-weight: bold; margin-left: 0.9em; } #wrapperlicious p { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious li > p { margin-bottom: 0; margin-top: 0; } #wrapperlicious pre { border: 1px solid #c1c1c1; border-radius: 3px; font: 100% Consolas, Menlo, Monaco, monospace; margin-bottom: 1em; margin-top: 1em; } #wrapperlicious pre > code { display: block; background-color: #f6f6f6; font: 0.9em Consolas, Menlo, Monaco, monospace; line-height: 1.5em; text-align: left; white-space: pre; padding: 1em; } #wrapperlicious dl, #wrapperlicious ol, #wrapperlicious ul { margin-bottom: 1em; margin-top: 1em; } #wrapperlicious ul { list-style-type: square; } #wrapperlicious ul ul { margin-bottom: 0px; margin-top: 0px; } #footer { font-size: 0.8em; padding-top: 0.5em; text-align: center; } #more { display: inline; font-size: 0.8em; } #perldocdiv { background-color: #fff; border: 1px solid #c1c1c1; border-bottom-left-radius: 5px; border-bottom-right-radius: 5px; margin-left: auto; margin-right: auto; padding: 3em; padding-top: 1em; max-width: 960px; } #moduleversion { float: right } #wrapperlicious .leading-notice { font-style: italic; padding-left: 1em; margin-top: 1em; margin-bottom: 1em; } #wrapperlicious .permalink { display: none; left: -0.75em; position: absolute; padding-right: 0.25em; text-decoration: none; } #wrapperlicious h1:hover .permalink, #wrapperlicious h2:hover .permalink, #wrapperlicious h3:hover .permalink, #wrapperlicious h4:hover .permalink, #wrapperlicious dt:hover .permalink { display: block; } </style> <!-- Global site tag (gtag.js) - Google Analytics --> <script async src="https://www.googletagmanager.com/gtag/js?id=G-KVNWBNT5FB"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-KVNWBNT5FB'); gtag('config', 'UA-50555-3'); </script> </head> <body> <nav class="navbar navbar-expand-md navbar-dark bg-dark justify-content-between"> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <a class="navbar-brand" href="/"><img src="/images/perl_camel_30.png" width="30" height="30" class="d-inline-block align-top" alt="Perl Camel Logo"> Perldoc Browser</a> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav mr-auto"> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-stable" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">5.24.2</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-stable"> <a class="dropdown-item" href="/perllol">Latest</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1/perllol">5.40.1</a> <a class="dropdown-item" href="/5.40.0/perllol">5.40.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3/perllol">5.38.3</a> <a class="dropdown-item" href="/5.38.2/perllol">5.38.2</a> <a class="dropdown-item" href="/5.38.1/perllol">5.38.1</a> <a class="dropdown-item" href="/5.38.0/perllol">5.38.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.36.3/perllol">5.36.3</a> <a class="dropdown-item" href="/5.36.2/perllol">5.36.2</a> <a class="dropdown-item" href="/5.36.1/perllol">5.36.1</a> <a class="dropdown-item" href="/5.36.0/perllol">5.36.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.34.3/perllol">5.34.3</a> <a class="dropdown-item" href="/5.34.2/perllol">5.34.2</a> <a class="dropdown-item" href="/5.34.1/perllol">5.34.1</a> <a class="dropdown-item" href="/5.34.0/perllol">5.34.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.32.1/perllol">5.32.1</a> <a class="dropdown-item" href="/5.32.0/perllol">5.32.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.30.3/perllol">5.30.3</a> <a class="dropdown-item" href="/5.30.2/perllol">5.30.2</a> <a class="dropdown-item" href="/5.30.1/perllol">5.30.1</a> <a class="dropdown-item" href="/5.30.0/perllol">5.30.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.28.3/perllol">5.28.3</a> <a class="dropdown-item" href="/5.28.2/perllol">5.28.2</a> <a class="dropdown-item" href="/5.28.1/perllol">5.28.1</a> <a class="dropdown-item" href="/5.28.0/perllol">5.28.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.26.3/perllol">5.26.3</a> <a class="dropdown-item" href="/5.26.2/perllol">5.26.2</a> <a class="dropdown-item" href="/5.26.1/perllol">5.26.1</a> <a class="dropdown-item" href="/5.26.0/perllol">5.26.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.4/perllol">5.24.4</a> <a class="dropdown-item" href="/5.24.3/perllol">5.24.3</a> <a class="dropdown-item active" href="/5.24.2/perllol">5.24.2</a> <a class="dropdown-item" href="/5.24.1/perllol">5.24.1</a> <a class="dropdown-item" href="/5.24.0/perllol">5.24.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.22.4/perllol">5.22.4</a> <a class="dropdown-item" href="/5.22.3/perllol">5.22.3</a> <a class="dropdown-item" href="/5.22.2/perllol">5.22.2</a> <a class="dropdown-item" href="/5.22.1/perllol">5.22.1</a> <a class="dropdown-item" href="/5.22.0/perllol">5.22.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.20.3/perllol">5.20.3</a> <a class="dropdown-item" href="/5.20.2/perllol">5.20.2</a> <a class="dropdown-item" href="/5.20.1/perllol">5.20.1</a> <a class="dropdown-item" href="/5.20.0/perllol">5.20.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.18.4/perllol">5.18.4</a> <a class="dropdown-item" href="/5.18.3/perllol">5.18.3</a> <a class="dropdown-item" href="/5.18.2/perllol">5.18.2</a> <a class="dropdown-item" href="/5.18.1/perllol">5.18.1</a> <a class="dropdown-item" href="/5.18.0/perllol">5.18.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.16.3/perllol">5.16.3</a> <a class="dropdown-item" href="/5.16.2/perllol">5.16.2</a> <a class="dropdown-item" href="/5.16.1/perllol">5.16.1</a> <a class="dropdown-item" href="/5.16.0/perllol">5.16.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.14.4/perllol">5.14.4</a> <a class="dropdown-item" href="/5.14.3/perllol">5.14.3</a> <a class="dropdown-item" href="/5.14.2/perllol">5.14.2</a> <a class="dropdown-item" href="/5.14.1/perllol">5.14.1</a> <a class="dropdown-item" href="/5.14.0/perllol">5.14.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.12.5/perllol">5.12.5</a> <a class="dropdown-item" href="/5.12.4/perllol">5.12.4</a> <a class="dropdown-item" href="/5.12.3/perllol">5.12.3</a> <a class="dropdown-item" href="/5.12.2/perllol">5.12.2</a> <a class="dropdown-item" href="/5.12.1/perllol">5.12.1</a> <a class="dropdown-item" href="/5.12.0/perllol">5.12.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.10.1/perllol">5.10.1</a> <a class="dropdown-item" href="/5.10.0/perllol">5.10.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.8.9/perllol">5.8.9</a> <a class="dropdown-item" href="/5.8.8/perllol">5.8.8</a> <a class="dropdown-item" href="/5.8.7/perllol">5.8.7</a> <a class="dropdown-item" href="/5.8.6/perllol">5.8.6</a> <a class="dropdown-item" href="/5.8.5/perllol">5.8.5</a> <a class="dropdown-item" href="/5.8.4/perllol">5.8.4</a> <a class="dropdown-item" href="/5.8.3/perllol">5.8.3</a> <a class="dropdown-item" href="/5.8.2/perllol">5.8.2</a> <a class="dropdown-item" href="/5.8.1/perllol">5.8.1</a> <a class="dropdown-item" href="/5.8.0/perllol">5.8.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/perllol">5.6.2</a> <a class="dropdown-item" href="/5.6.1/perllol">5.6.1</a> <a class="dropdown-item" href="/5.6.0/perllol">5.6.0</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.005_04/perllol">5.005_04</a> <a class="dropdown-item" href="/5.005_03/perllol">5.005_03</a> <a class="dropdown-item" href="/5.005_02/perllol">5.005_02</a> <a class="dropdown-item" href="/5.005_01/perllol">5.005_01</a> <a class="dropdown-item" href="/5.005/perllol">5.005</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-dev" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Dev</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-dev"> <a class="dropdown-item" href="/blead/perllol">blead</a> <a class="dropdown-item" href="/5.41.9/perllol">5.41.9</a> <a class="dropdown-item" href="/5.41.8/perllol">5.41.8</a> <a class="dropdown-item" href="/5.41.7/perllol">5.41.7</a> <a class="dropdown-item" href="/5.41.6/perllol">5.41.6</a> <a class="dropdown-item" href="/5.41.5/perllol">5.41.5</a> <a class="dropdown-item" href="/5.41.4/perllol">5.41.4</a> <a class="dropdown-item" href="/5.41.3/perllol">5.41.3</a> <a class="dropdown-item" href="/5.41.2/perllol">5.41.2</a> <a class="dropdown-item" href="/5.41.1/perllol">5.41.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.40.1-RC1/perllol">5.40.1-RC1</a> <a class="dropdown-item" href="/5.40.0-RC2/perllol">5.40.0-RC2</a> <a class="dropdown-item" href="/5.40.0-RC1/perllol">5.40.0-RC1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.39.10/perllol">5.39.10</a> <a class="dropdown-item" href="/5.39.9/perllol">5.39.9</a> <a class="dropdown-item" href="/5.39.8/perllol">5.39.8</a> <a class="dropdown-item" href="/5.39.7/perllol">5.39.7</a> <a class="dropdown-item" href="/5.39.6/perllol">5.39.6</a> <a class="dropdown-item" href="/5.39.5/perllol">5.39.5</a> <a class="dropdown-item" href="/5.39.4/perllol">5.39.4</a> <a class="dropdown-item" href="/5.39.3/perllol">5.39.3</a> <a class="dropdown-item" href="/5.39.2/perllol">5.39.2</a> <a class="dropdown-item" href="/5.39.1/perllol">5.39.1</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.38.3-RC1/perllol">5.38.3-RC1</a> </div> </li> <li class="nav-item dropdown text-nowrap"> <a class="nav-link dropdown-toggle" href="#" id="dropdownlink-nav" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Documentation</a> <div class="dropdown-menu" aria-labelledby="dropdownlink-nav"> <a class="dropdown-item" href="/5.24.2/perl">Perl</a> <a class="dropdown-item" href="/5.24.2/perlintro">Intro</a> <a class="dropdown-item" href="/5.24.2/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.24.2/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.24.2/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.2/perlop">Operators</a> <a class="dropdown-item" href="/5.24.2/functions">Functions</a> <a class="dropdown-item" href="/5.24.2/variables">Variables</a> <a class="dropdown-item" href="/5.24.2/modules">Modules</a> <a class="dropdown-item" href="/5.24.2/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.24.2/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.24.2/perlcommunity">Community</a> <a class="dropdown-item" href="/5.24.2/perlhist">History</a> </div> </li> </ul> <ul class="navbar-nav"> <script> function set_expand (expand) { var perldocdiv = document.getElementById('perldocdiv'); var width = window.getComputedStyle(perldocdiv).getPropertyValue('max-width'); var expanded = (width == '' || width == 'none') ? true : false; if (expand === null) { expand = !expanded; } if ((expand && !expanded) || (!expand && expanded)) { perldocdiv.style.setProperty('max-width', expand ? 'none' : '960px'); var button_classlist = document.getElementById('content-expand-button').classList; if (expand) { button_classlist.add('btn-light'); button_classlist.remove('btn-outline-light'); } else { button_classlist.add('btn-outline-light'); button_classlist.remove('btn-light'); } } return expand; } function toggle_expand () { var expand = set_expand(null); document.cookie = 'perldoc_expand=' + (expand ? 1 : 0) + '; path=/; expires=Tue, 19 Jan 2038 03:14:07 UTC'; } function read_expand () { return document.cookie.split(';').some(function (item) { return item.indexOf('perldoc_expand=1') >= 0 }); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', function () { if (read_expand()) { set_expand(true); } }); } else if (read_expand()) { set_expand(true); } </script> <button id="content-expand-button" type="button" class="btn btn-outline-light d-none d-lg-inline-block mr-4" onclick="toggle_expand()">Expand</button> </ul> <form class="form-inline" method="get" action="/5.24.2/search"> <input class="form-control mr-3" type="search" name="q" placeholder="Search" aria-label="Search" value=""> </form> </div> </nav> <div id="wrapperlicious" class="container-fluid"> <div id="perldocdiv"> <div id="links"> <a href="/5.24.2/perllol">perllol</a> <div id="more"> (<a href="/5.24.2/perllol.txt">source</a>, <a href="https://metacpan.org/pod/perllol">CPAN</a>) </div> </div> <div class="leading-notice"> You are viewing the version of this documentation from Perl 5.24.2. <a href="/perllol">View the latest version</a> </div> <h1><a id="toc">CONTENTS</a></h1> <ul> <li> <a class="text-decoration-none" href="#NAME">NAME</a> </li> <li> <a class="text-decoration-none" href="#DESCRIPTION">DESCRIPTION</a> <ul> <li> <a class="text-decoration-none" href="#Declaration-and-Access-of-Arrays-of-Arrays">Declaration and Access of Arrays of Arrays</a> </li> <li> <a class="text-decoration-none" href="#Growing-Your-Own">Growing Your Own</a> </li> <li> <a class="text-decoration-none" href="#Access-and-Printing">Access and Printing</a> </li> <li> <a class="text-decoration-none" href="#Slices">Slices</a> </li> </ul> </li> <li> <a class="text-decoration-none" href="#SEE-ALSO">SEE ALSO</a> </li> <li> <a class="text-decoration-none" href="#AUTHOR">AUTHOR</a> </li> </ul> <h1 id="NAME"><a class="permalink" href="#NAME">#</a>NAME</h1> <p>perllol - Manipulating Arrays of Arrays in Perl</p> <h1 id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">#</a>DESCRIPTION</h1> <h2 id="Declaration-and-Access-of-Arrays-of-Arrays"><a class="permalink" href="#Declaration-and-Access-of-Arrays-of-Arrays">#</a><a id="Declaration"></a>Declaration and Access of Arrays of Arrays</h2> <p>The simplest two-level data structure to build in Perl is an array of arrays, sometimes casually called a list of lists. It&#39;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&#39;s a declaration of the array:</p> <pre><code> use 5.010; # so we can use say() # assign to our array, an array of array references @AoA = ( [ &quot;fred&quot;, &quot;barney&quot;, &quot;pebbles&quot;, &quot;bambam&quot;, &quot;dino&quot;, ], [ &quot;george&quot;, &quot;jane&quot;, &quot;elroy&quot;, &quot;judy&quot;, ], [ &quot;homer&quot;, &quot;bart&quot;, &quot;marge&quot;, &quot;maggie&quot;, ], ); say $AoA[2][1]; bart</code></pre> <p>Now you should be very careful that the outer bracket type is a round one, that is, a parenthesis. That&#39;s because you&#39;re assigning to an @array, so you need parentheses. If you wanted there <i>not</i> to be an @AoA, but rather just a reference to it, you could do something more like this:</p> <pre><code> # assign a reference to array of array references $ref_to_AoA = [ [ &quot;fred&quot;, &quot;barney&quot;, &quot;pebbles&quot;, &quot;bambam&quot;, &quot;dino&quot;, ], [ &quot;george&quot;, &quot;jane&quot;, &quot;elroy&quot;, &quot;judy&quot;, ], [ &quot;homer&quot;, &quot;bart&quot;, &quot;marge&quot;, &quot;maggie&quot;, ], ]; say $ref_to_AoA-&gt;[2][1]; bart</code></pre> <p>Notice that the outer bracket type has changed, and so our access syntax has also changed. That&#39;s because unlike C, in perl you can&#39;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><code>$AoA[2][2] $ref_to_AoA-&gt;[2][2]</code></pre> <p>instead of having to write these:</p> <pre><code>$AoA[2]-&gt;[2] $ref_to_AoA-&gt;[2]-&gt;[2]</code></pre> <p>Well, that&#39;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&#39;s a scalar containing a reference, which means that $ref_to_AoA always needs it.</p> <h2 id="Growing-Your-Own"><a class="permalink" href="#Growing-Your-Own">#</a><a id="Growing"></a>Growing Your Own</h2> <p>That&#39;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&#39;s look at reading it in from a file. This is something like adding a row at a time. We&#39;ll assume that there&#39;s a flat file in which each line is a row and each word an element. If you&#39;re trying to develop an @AoA array containing all these, here&#39;s the right way to do that:</p> <pre><code> while (&lt;&gt;) { @tmp = split; push @AoA, [ @tmp ]; }</code></pre> <p>You might also have loaded that from a function:</p> <pre><code> for $i ( 1 .. 10 ) { $AoA[$i] = [ somefunc($i) ]; }</code></pre> <p>Or you might have had a temporary variable sitting around with the array in it.</p> <pre><code> for $i ( 1 .. 10 ) { @tmp = somefunc($i); $AoA[$i] = [ @tmp ]; }</code></pre> <p>It&#39;s important you make sure to use the <code>[ ]</code> array reference constructor. That&#39;s because this wouldn&#39;t work:</p> <pre><code>$AoA[$i] = @tmp; # WRONG!</code></pre> <p>The reason that doesn&#39;t do what you want is because assigning a named array like that to a scalar is taking an array in scalar context, which means just counts the number of elements in @tmp.</p> <p>If you are running under <code>use strict</code> (and if you aren&#39;t, why in the world aren&#39;t you?), you&#39;ll have to add some declarations to make it happy:</p> <pre><code> use strict; my(@AoA, @tmp); while (&lt;&gt;) { @tmp = split; push @AoA, [ @tmp ]; }</code></pre> <p>Of course, you don&#39;t need the temporary array to have a name at all:</p> <pre><code> while (&lt;&gt;) { push @AoA, [ split ]; }</code></pre> <p>You also don&#39;t have to use push(). You could just make a direct assignment if you knew where you wanted to put it:</p> <pre><code> my (@AoA, $i, $line); for $i ( 0 .. 10 ) { $line = &lt;&gt;; $AoA[$i] = [ split &quot; &quot;, $line ]; }</code></pre> <p>or even just</p> <pre><code> my (@AoA, $i); for $i ( 0 .. 10 ) { $AoA[$i] = [ split &quot; &quot;, &lt;&gt; ]; }</code></pre> <p>You should in general be leery of using functions that could potentially return lists in scalar context without explicitly stating such. This would be clearer to the casual reader:</p> <pre><code> my (@AoA, $i); for $i ( 0 .. 10 ) { $AoA[$i] = [ split &quot; &quot;, scalar(&lt;&gt;) ]; }</code></pre> <p>If you wanted to have a $ref_to_AoA variable as a reference to an array, you&#39;d have to do something like this:</p> <pre><code> while (&lt;&gt;) { push @$ref_to_AoA, [ split ]; }</code></pre> <p>Now you can add new rows. What about adding new columns? If you&#39;re dealing with just matrices, it&#39;s often easiest to use simple assignment:</p> <pre><code> for $x (1 .. 10) { for $y (1 .. 10) { $AoA[$x][$y] = func($x, $y); } } for $x ( 3, 7, 9 ) { $AoA[$x][20] += func2($x); }</code></pre> <p>It doesn&#39;t matter whether those elements are already there or not: it&#39;ll gladly create them for you, setting intervening elements to <code>undef</code> as need be.</p> <p>If you wanted just to append to a row, you&#39;d have to do something a bit funnier looking:</p> <pre><code># add new columns to an existing row push @{ $AoA[0] }, &quot;wilma&quot;, &quot;betty&quot;; # explicit deref</code></pre> <h2 id="Access-and-Printing"><a class="permalink" href="#Access-and-Printing">#</a><a id="Access"></a>Access and Printing</h2> <p>Now it&#39;s time to print your data structure out. How are you going to do that? Well, if you want only one of the elements, it&#39;s trivial:</p> <pre><code>print $AoA[0][0];</code></pre> <p>If you want to print the whole thing, though, you can&#39;t say</p> <pre><code>print @AoA; # WRONG</code></pre> <p>because you&#39;ll get just references listed, and perl will never automatically dereference things for you. Instead, you have to roll yourself a loop or two. This prints the whole structure, using the shell-style for() construct to loop across the outer set of subscripts.</p> <pre><code> for $aref ( @AoA ) { say &quot;\t [ @$aref ],&quot;; }</code></pre> <p>If you wanted to keep track of subscripts, you might do this:</p> <pre><code> for $i ( 0 .. $#AoA ) { say &quot;\t elt $i is [ @{$AoA[$i]} ],&quot;; }</code></pre> <p>or maybe even this. Notice the inner loop.</p> <pre><code> for $i ( 0 .. $#AoA ) { for $j ( 0 .. $#{$AoA[$i]} ) { say &quot;elt $i $j is $AoA[$i][$j]&quot;; } }</code></pre> <p>As you can see, it&#39;s getting a bit complicated. That&#39;s why sometimes is easier to take a temporary on your way through:</p> <pre><code> for $i ( 0 .. $#AoA ) { $aref = $AoA[$i]; for $j ( 0 .. $#{$aref} ) { say &quot;elt $i $j is $AoA[$i][$j]&quot;; } }</code></pre> <p>Hmm... that&#39;s still a bit ugly. How about this:</p> <pre><code> for $i ( 0 .. $#AoA ) { $aref = $AoA[$i]; $n = @$aref - 1; for $j ( 0 .. $n ) { say &quot;elt $i $j is $AoA[$i][$j]&quot;; } }</code></pre> <p>When you get tired of writing a custom print for your data structures, you might look at the standard <a href="/5.24.2/Dumpvalue">Dumpvalue</a> or <a href="/5.24.2/Data::Dumper">Data::Dumper</a> modules. The former is what the Perl debugger uses, while the latter generates parsable Perl code. For example:</p> <pre><code> use v5.14; # using the + prototype, new to v5.14 sub show(+) { require Dumpvalue; state $prettily = new Dumpvalue:: tick =&gt; q(&quot;), compactDump =&gt; 1, # comment these two lines # out veryCompact =&gt; 1, # if you want a bigger # dump ; dumpValue $prettily @_; } # Assign a list of array references to an array. my @AoA = ( [ &quot;fred&quot;, &quot;barney&quot; ], [ &quot;george&quot;, &quot;jane&quot;, &quot;elroy&quot; ], [ &quot;homer&quot;, &quot;marge&quot;, &quot;bart&quot; ], ); push @{ $AoA[0] }, &quot;wilma&quot;, &quot;betty&quot;; show @AoA;</code></pre> <p>will print out:</p> <pre><code class="plaintext">0 0..3 &quot;fred&quot; &quot;barney&quot; &quot;wilma&quot; &quot;betty&quot; 1 0..2 &quot;george&quot; &quot;jane&quot; &quot;elroy&quot; 2 0..2 &quot;homer&quot; &quot;marge&quot; &quot;bart&quot;</code></pre> <p>Whereas if you comment out the two lines I said you might wish to, then it shows it to you this way instead:</p> <pre><code class="plaintext">0 ARRAY(0x8031d0) 0 &quot;fred&quot; 1 &quot;barney&quot; 2 &quot;wilma&quot; 3 &quot;betty&quot; 1 ARRAY(0x803d40) 0 &quot;george&quot; 1 &quot;jane&quot; 2 &quot;elroy&quot; 2 ARRAY(0x803e10) 0 &quot;homer&quot; 1 &quot;marge&quot; 2 &quot;bart&quot;</code></pre> <h2 id="Slices"><a class="permalink" href="#Slices">#</a>Slices</h2> <p>If you want to get at a slice (part of a row) in a multidimensional array, you&#39;re going to have to do some fancy subscripting. That&#39;s because while we have a nice synonym for single elements via the pointer arrow for dereferencing, no such convenience exists for slices.</p> <p>Here&#39;s how to do one operation using a loop. We&#39;ll assume an @AoA variable as before.</p> <pre><code> @part = (); $x = 4; for ($y = 7; $y &lt; 13; $y++) { push @part, $AoA[$x][$y]; }</code></pre> <p>That same loop could be replaced with a slice operation:</p> <pre><code>@part = @{$AoA[4]}[7..12];</code></pre> <p>or spaced out a bit:</p> <pre><code>@part = @{ $AoA[4] } [ 7..12 ];</code></pre> <p>But as you might well imagine, this can get pretty rough on the reader.</p> <p>Ah, but what if you wanted a <i>two-dimensional slice</i>, such as having $x run from 4..8 and $y run from 7 to 12? Hmm... here&#39;s the simple way:</p> <pre><code> @newAoA = (); for ($startx = $x = 4; $x &lt;= 8; $x++) { for ($starty = $y = 7; $y &lt;= 12; $y++) { $newAoA[$x - $startx][$y - $starty] = $AoA[$x][$y]; } }</code></pre> <p>We can reduce some of the looping through slices</p> <pre><code> for ($x = 4; $x &lt;= 8; $x++) { push @newAoA, [ @{ $AoA[$x] } [ 7..12 ] ]; }</code></pre> <p>If you were into Schwartzian Transforms, you would probably have selected map for that</p> <pre><code>@newAoA = map { [ @{ $AoA[$_] } [ 7..12 ] ] } 4 .. 8;</code></pre> <p>Although if your manager accused you of seeking job security (or rapid insecurity) through inscrutable code, it would be hard to argue. :-) If I were you, I&#39;d put that in a function:</p> <pre><code> @newAoA = splice_2D( \@AoA, 4 =&gt; 8, 7 =&gt; 12 ); sub splice_2D { my $lrr = shift; # ref to array of array refs! my ($x_lo, $x_hi, $y_lo, $y_hi) = @_; return map { [ @{ $lrr-&gt;[$_] } [ $y_lo .. $y_hi ] ] } $x_lo .. $x_hi; }</code></pre> <h1 id="SEE-ALSO"><a class="permalink" href="#SEE-ALSO">#</a><a id="SEE"></a>SEE ALSO</h1> <p><a href="/5.24.2/perldata">perldata</a>, <a href="/5.24.2/perlref">perlref</a>, <a href="/5.24.2/perldsc">perldsc</a></p> <h1 id="AUTHOR"><a class="permalink" href="#AUTHOR">#</a>AUTHOR</h1> <p>Tom Christiansen &lt;<i>tchrist@perl.com</i>&gt;</p> <p>Last update: Tue Apr 26 18:30:55 MDT 2011</p> </div> <div id="footer"> <p>Perldoc Browser is maintained by Dan Book (<a href="https://metacpan.org/author/DBOOK">DBOOK</a>). Please contact him via the <a href="https://github.com/Grinnz/perldoc-browser/issues">GitHub issue tracker</a> or <a href="mailto:dbook@cpan.org">email</a> regarding any issues with the site itself, search, or rendering of documentation.</p> <p>The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via the <a href="https://github.com/Perl/perl5/issues">Perl issue tracker</a>, the <a href="https://lists.perl.org/list/perl5-porters.html">mailing list</a>, or <a href="https://kiwiirc.com/client/irc.perl.org/p5p">IRC</a> to report any issues with the contents or format of the documentation.</p> </div> </div> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.slim.min.js" integrity="sha512-/DXTXr6nQodMUiq+IUJYCt2PPOUjrHJ9wFrqpJ3XkgPNOZVfMok7cRw6CSxyCQxXn6ozlESsSh1/sMCTF1rL/g==" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.1/umd/popper.min.js" integrity="sha512-ubuT8Z88WxezgSqf3RLuNi5lmjstiJcyezx34yIU2gAHonIi27Na7atqzUZCOoY4CExaoFumzOsFQ2Ch+I/HCw==" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js" integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV" crossorigin="anonymous"></script> <script src="/js/highlight.pack.js"></script> <script>hljs.highlightAll();</script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10