Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The Problem is that you aren't properly declaring your variables. For every script, you should</p> <pre class="lang-perl prettyprint-override"><code>use strict; use warnings; </code></pre> <p>This disallows common error sources, warns about iffy stuff, and forces you to properly declare all your variables.</p> <p>By default, all undeclared variables are considered <em>global</em>. Therefore, in</p> <pre><code>for($i=0;$i&lt;=9;$i++) { @Space = (); push(@Hits,\@Space); } </code></pre> <p>the <code>@Space</code> refers to the same array in each iteration. Ergo, all ten entries in <code>@Hits</code> are a reference to the same array. Let's inspect what <code>@Hits</code> actually is. We can do so with <code>Data::Dumper</code> or the <code>Data::Dump</code> module (the latter usually produces prettier output):</p> <pre><code>use Data::Dump; # use Data::Dumper; dd \@Hits; # print Dumper \@Hits; </code></pre> <p>We get with <code>Data::Dumper</code> (easier to understand):</p> <pre><code>$VAR1 = [ [ 1, 3, 5, 7 ], $VAR1-&gt;[0], $VAR1-&gt;[0], $VAR1-&gt;[0], $VAR1-&gt;[0], $VAR1-&gt;[0], $VAR1-&gt;[0], $VAR1-&gt;[0], $VAR1-&gt;[0], $VAR1-&gt;[0] ]; </code></pre> <hr> <p>So I said the solution would be declaring your variables. Specifically, we want <em>lexical variables</em>. These variables are only visible <em>inside</em> the block where they are declared. This makes reasoning about code much easier. We can declare a lexical variable like so:</p> <pre><code>my $foo = 123; </code></pre> <p>When we have a loop like</p> <pre><code>my @Hits; for my $i (0 .. 9) { my @Space; push @Hits, \@Space; } </code></pre> <p>then each time the <code>my</code> is executed, we get a <em>new</em> <code>@Space</code>. Oh, and I have used a foreach-loop, that iterates over the range <code>0 .. 9</code> with the (lexical) <code>$i</code> variable. I find this easier to understand than the C-style loops you used.</p> <p>Because every element in <code>@Hits</code> now is a <em>different</em> arrayref, we get the expected data structure. As <code>Data::Dump</code> output:</p> <pre><code>[[], [1], [], [3], [], [5], [], [7], [], []] </code></pre> <p>When we now execute your loop that prints out the first value of each sub-array, then you may be suprised by the empty lines in between the numebers. This is because e.g. the first arrayref does not have an entry at index <code>0</code>, and therefore returns the special <code>undef</code> value. When used as a string, this is the empty string. If you followed my advice and did the <code>use warnings</code>, you also get a message that you are printing an uninitialized value.</p> <p>We can solve this by testing for definedness, and providing a zero otherwise. Since perl5 v10, we can use the defined-or operator <code>//</code> for this (on earlier perls, the <code>||</code> logical or has to do).</p> <pre><code>for my $i (0 .. 9) { my $value = $Hits[$i][0] // 0; print "$value\n"; } </code></pre> <p>There are a few other bits we can improve:</p> <ul> <li>We don't have to manually create the <code>@Space</code> arrays; Perl does this behind the scenes when you <em>dereference</em> an array entry like <code>@{ $Hits[$i] }</code>. This is called <em>autovivification</em>.</li> <li>Not only can we iterate over ranges, but also over arrays. This is much better than hardcoding indices.</li> <li>Since v10, you can use the <code>say</code> feature. The <code>say</code> function is exactly like <code>print</code> but appends a newline at the end.</li> </ul> <p>Here is how I'd have written that code:</p> <pre><code>#!/usr/bin/perl use strict; use warnings; use feature 'say'; my @Hits; for my $i (1, 3, 5, 7) { push @{ $Hits[$i] }, $i; } for my $arrayref (@Hits) { say $arrayref-&gt;[0] // 0; } </code></pre> <p>Output:</p> <pre><code>0 1 0 3 0 5 0 7 </code></pre> <p>(Note that we never initialized values at positions 8 and 9, so they are not shown. We could amend this by iterating over the <em>slice</em> <code>@Hits[0 .. 9]</code>.)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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