Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Errors:</strong></p> <pre><code>for($i=0;$i&lt;=5;$i++) $values{$proposal[$i]}[$i]++; for($i=$proposal[1];$i&lt;=$proposal[2];$i++) $values{$i}[6]++; </code></pre> <p>Perl does not support bare loop/conditional blocks. Or rather, <a href="http://perldoc.perl.org/perlsyn.html#Statement-Modifiers" rel="nofollow noreferrer">it does</a>, but not like this. This may work in PHP, but not in Perl. You will want to enclose these in blocks:</p> <pre><code>for($i=0;$i&lt;=5;$i++) { $values{$proposal[$i]}[$i]++; } for($i=$proposal[1];$i&lt;=$proposal[2];$i++) { $values{$i}[6]++; } </code></pre> <hr> <pre><code>$values{$proposal[$i]}[$i]++; </code></pre> <p>Since hashes in Perl can only fit scalar data types in them, in order to store an entire array inside of a hash, we're going to have to do it by reference. Here's a quick tutorial on array references:</p> <pre><code>my $arr_ref = []; # empty array reference my $arr_ref = [ 1, 2, 'foo', ]; # initialize with values my $arr_ref = \@arr; # reference an existing array; # does not make copy, but provides a # read-write handle to the array $arr_ref-&gt;[0]; # index the first (index 0) element of the array @{$arr_ref}[ 0 .. 4 ]; # index elements number one through five (0-4) of the array # through what's called an "array slice" </code></pre> <p>What your code above does is pull the value at hash key <code>$proposal[$i]</code> out of the hash <code>%values</code>, then use it (a scalar) as an array (it is not an array).</p> <p>As I said before, you can use it as an array <em>reference</em> but not an array:</p> <pre><code> # v-- note the arrow $values{$proposal[$i]}-&gt;[$i]++; </code></pre> <p><strong>Suggestions:</strong></p> <ul> <li><p>Writing <code>my $foo; for ($foo = 0; $foo &lt;= 5; $foo++)</code> is more easily written as "<code>for my $foo (0 .. 5)</code>" or "<code>foreach my $foo (0 .. 5)</code>". This is, in essence how most people do it. Of note is that <code>for</code> and <code>foreach</code> are interchangeable&ndash;it's a matter of preference and legibility.</p></li> <li><p>Please, for legibility's sake, indent your code with more than one space. A good rule of thumb is four spaces, or a tab. St. Larry Wall was thinking of languages people speak and write when he designed Perl.</p></li> <li><p>I'd recommend researching the proper (proper, here, meaning most efficient) way to write <code>for</code> loops. There are a few habits that can result in faster programs overall if they have a lot of long <code>for</code> loops. For instance:</p> <ul> <li><code>++$foo</code> is more efficient than <code>$foo++</code>. This stems from the internals: <ul> <li><code>$foo++</code> increments the variable, subtracts 1 from it, then returns the result, whereas</li> <li><code>++$foo</code> increments the variable and returns it. Fewer operations = faster.</li> </ul></li> <li>A less-than-or-equals comparison is less efficient than a plain less-than comparison. Again, this is due to the number of operations you computer has to perform. <code>for ($x=0; $x&lt;=5; ++$x)</code> is better-written as <code>for ($x=0; $x&lt;6; ++$x)</code>.</li> </ul></li> <li><p>Perl has some wonderful loop controls. Some, like <code>map</code>, are very powerful.</p></li> </ul>
    singulars
    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.
 

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