Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Perl doesn't have <code>null</code>, but it has <code>undef</code> – which incidentally is the default value for unassigned elements in arrays:</p> <pre><code>my @array = (undef, undef, 32, 54, undef, 59); </code></pre> <p>creates the same structure as:</p> <pre><code>my @array; $array[2] = 32; $array[3] = 54; $array[5] = 59; </code></pre> <p>There is no need to assign the other fields to <code>undef</code> yourself.</p> <p>If you are trying to assign a default value to elements which are currently <code>undef</code>, you could write something like this (but note the limitations below):</p> <pre><code>defined or $_ = "default" for @array; </code></pre> <p>On Perl 5.10 or later, you can use the <code>//</code> defined-or operator:</p> <pre><code>$_ //= "default" for @array </code></pre> <p>To assign defaults for multiple arrays:</p> <pre><code>$_ //= "default" for @array, @other_array </code></pre> <p>If you want to set an array to a specific length, you can do <code>$#array = $length - 1</code>, so this actually specifies the highest index. This removes entries from longer arrays. For shorter arrays, the newly created entries will all be <code>undef</code>.</p> <hr> <p>There is a small problem with this: Perl has two kinds of <code>undef</code>:</p> <ul> <li><p>Scalars can contain the value “undef” like <code>my $foo = undef</code>.</p> <p>This is the case if we initialize the whole array at once like <code>my @array = (undef, undef, 32, 54, undef, 59)</code>.</p></li> <li><p>Unassigned values in arrays or hashes <em>share</em> their <code>undef</code> scalar, which is read-only.</p> <p>This is the case when we initialize the array by assigning some indices only, like <code>$array[4] = 2</code>.</p></li> </ul> <p>Usually this is no problem, but in a for-loop, the <code>$_</code> is an <em>alias</em> to the current scalar, which in our case can be readonly. Therefore we can't always do <code>$_ //= "default" for @array</code> but must either:</p> <ul> <li><p>Assign directly into the array element, which creates an assignable scalar in that slot:</p> <pre><code>$array[$_] //= "default" for 0 .. $#array; </code></pre> <p>For multiple arrays:</p> <pre><code>for my $ref (\@array, \@other_array) { $ref-&gt;[$_] //= "default" for 0 .. $#$ref; } </code></pre></li> <li><p>Don't assign to the element and create a copy of the array with defaults instead:</p> <pre><code>@array = map { $_ // "default" } @array; </code></pre> <p>For multiple arrays:</p> <pre><code>@$_ = map { $_ // "default" } @$_ for \@array, \@other_array; </code></pre> <p>While I perceive this as more elegant, there can be some problems with this as each array element is copied.</p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload