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.6.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" 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 active" 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.6.2/perl">Perl</a> <a class="dropdown-item" href="/5.6.2/perlintro">Intro</a> <a class="dropdown-item" href="/5.6.2/perl#Tutorials">Tutorials</a> <a class="dropdown-item" href="/5.6.2/perlfaq">FAQs</a> <a class="dropdown-item" href="/5.6.2/perl#Reference-Manual">Reference</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/perlop">Operators</a> <a class="dropdown-item" href="/5.6.2/functions">Functions</a> <a class="dropdown-item" href="/5.6.2/variables">Variables</a> <a class="dropdown-item" href="/5.6.2/modules">Modules</a> <a class="dropdown-item" href="/5.6.2/perlutil">Utilities</a> <div class="dropdown-divider"></div> <a class="dropdown-item" href="/5.6.2/perldelta">Release Notes</a> <a class="dropdown-item" href="/5.6.2/perlcommunity">Community</a> <a class="dropdown-item" href="/5.6.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.6.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.6.2/perllol">perllol</a> <div id="more"> (<a href="/5.6.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.6.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 thing to build 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><code> # assign to our array, an array of array references @AoA = ( [ "fred", "barney" ], [ "george", "jane", "elroy" ], [ "homer", "marge", "bart" ], ); print $AoA[2][2]; bart</code></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 <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 = [ [ "fred", "barney", "pebbles", "bambam", "dino", ], [ "homer", "bart", "marge", "maggie", ], [ "george", "jane", "elroy", "judy", ], ]; print $ref_to_AoA->[2][2];</code></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><code>$AoA[2][2] $ref_to_AoA->[2][2]</code></pre> <p>instead of having to write these:</p> <pre><code>$AoA[2]->[2] $ref_to_AoA->[2]->[2]</code></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> <h2 id="Growing-Your-Own"><a class="permalink" href="#Growing-Your-Own">#</a><a id="Growing"></a>Growing Your Own</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><code> while (<>) { @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's very important that you make sure to use the <code>[]</code> array reference constructor. That's because this will be very wrong:</p> <pre><code>$AoA[$i] = @tmp;</code></pre> <p>You see, assigning a named array like that to a scalar just counts the number of elements in @tmp, which probably isn't what you want.</p> <p>If you are running under <code>use strict</code>, you'll have to add some declarations to make it happy:</p> <pre><code> use strict; my(@AoA, @tmp); while (<>) { @tmp = split; push @AoA, [ @tmp ]; }</code></pre> <p>Of course, you don't need the temporary array to have a name at all:</p> <pre><code> while (<>) { push @AoA, [ split ]; }</code></pre> <p>You also don'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 = <>; $AoA[$i] = [ split ' ', $line ]; }</code></pre> <p>or even just</p> <pre><code> my (@AoA, $i); for $i ( 0 .. 10 ) { $AoA[$i] = [ split ' ', <> ]; }</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 ' ', scalar(<>) ]; }</code></pre> <p>If you wanted to have a $ref_to_AoA variable as a reference to an array, you'd have to do something like this:</p> <pre><code> while (<>) { push @$ref_to_AoA, [ split ]; }</code></pre> <p>Now you can add new rows. What about adding new columns? If you're dealing with just matrices, it'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't matter whether those elements are already there or not: it'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'd have to do something a bit funnier looking:</p> <pre><code># add new columns to an existing row push @{ $AoA[0] }, "wilma", "betty";</code></pre> <p>Notice that I <i>couldn't</i> say just:</p> <pre><code>push $AoA[0], "wilma", "betty"; # WRONG!</code></pre> <p>In fact, that wouldn't even compile. How come? Because the argument to push() must be a real array, not just a reference to such.</p> <h2 id="Access-and-Printing"><a class="permalink" href="#Access-and-Printing">#</a><a id="Access"></a>Access and Printing</h2> <p>Now it'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's trivial:</p> <pre><code>print $AoA[0][0];</code></pre> <p>If you want to print the whole thing, though, you can't say</p> <pre><code>print @AoA; # WRONG</code></pre> <p>because you'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 ) { print "\t [ @$aref ],\n"; }</code></pre> <p>If you wanted to keep track of subscripts, you might do this:</p> <pre><code> for $i ( 0 .. $#AoA ) { print "\t elt $i is [ @{$AoA[$i]} ],\n"; }</code></pre> <p>or maybe even this. Notice the inner loop.</p> <pre><code> for $i ( 0 .. $#AoA ) { for $j ( 0 .. $#{$AoA[$i]} ) { print "elt $i $j is $AoA[$i][$j]\n"; } }</code></pre> <p>As you can see, it's getting a bit complicated. That'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} ) { print "elt $i $j is $AoA[$i][$j]\n"; } }</code></pre> <p>Hmm... that'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 ) { print "elt $i $j is $AoA[$i][$j]\n"; } }</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're going to have to do some fancy subscripting. That's because while we have a nice synonym for single elements via the pointer arrow for dereferencing, no such convenience exists for slices. (Remember, of course, that you can always write a loop to do a slice operation.)</p> <p>Here's how to do one operation using a loop. We'll assume an @AoA variable as before.</p> <pre><code> @part = (); $x = 4; for ($y = 7; $y < 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>but as you might well imagine, this is 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's the simple way:</p> <pre><code> @newAoA = (); for ($startx = $x = 4; $x <= 8; $x++) { for ($starty = $y = 7; $y <= 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 <= 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 of seeking job security (or rapid insecurity) through inscrutable code, it would be hard to argue. :-) If I were you, I'd put that in a function:</p> <pre><code> @newAoA = splice_2D( \@AoA, 4 => 8, 7 => 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->[$_] } [ $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>perldata(1), perlref(1), perldsc(1)</p> <h1 id="AUTHOR"><a class="permalink" href="#AUTHOR">#</a>AUTHOR</h1> <p>Tom Christiansen <<i>tchrist@perl.com</i>></p> <p>Last update: Thu Jun 4 16:16:23 MDT 1998</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>