<?xml version="1.0" encoding="windows-1252"?>
<node id="1007688" title="Re^2: Idiom to return 0 or random number of array elements" created="2012-12-07 00:46:27" updated="2012-12-07 00:46:27">
<type id="11">
note</type>
<author id="708738">
LanX</author>
<data>
<field name="doctext">
&lt;c&gt;@array[0 ..0]&lt;/c&gt; is one and not zero elements!&lt;P&gt;

&lt;C&gt;
  DB&lt;103&gt; @array=a..f
 =&gt; ("a", "b", "c", "d", "e", "f")
 
  DB&lt;104&gt; @array[0..0]
 =&gt; "a"
&lt;/c&gt;&lt;P&gt;


&lt;b&gt;UPDATE:&lt;/b&gt;&lt;P&gt;

 your statistical proof falls into the trap &lt;P&gt;

&lt;c&gt;scalar @array != scalar (LIST)&lt;/c&gt;&lt;P&gt;

if your &lt;c&gt;function()&lt;/c&gt; returns a one element list &lt;c&gt;(0)&lt;/c&gt;, then &lt;c&gt;scalar function()&lt;/c&gt; will be 0, since 0 is the last element of the list.&lt;P&gt;

(more detailed in [id://1007707] by [eyepopslikeamosquito] )&lt;P&gt;

initializing &lt;c&gt;@array = 1..10&lt;/c&gt; makes it more obvious, b/c the last element of a sublist is now identical to the length! &lt;P&gt;

&lt;c&gt;
  DB&lt;106&gt; @array = 1 .. 10;
 =&gt; (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
 
  DB&lt;107&gt; sub x{ return @array[ 0 .. int( rand @array ) ]; };
 =&gt; 0
 
  DB&lt;108&gt; $stats{ scalar( x() ) }++ for  1 .. 1000;
 =&gt; ""
 
  DB&lt;109&gt; \%stats;
 =&gt; { 1 =&gt; 100, 2 =&gt; 101, 3 =&gt; 108, 4 =&gt; 108, 5 =&gt; 101, 6 =&gt; 103, 7 =&gt; 79, 8 =&gt; 98, 9 =&gt; 100, 10 =&gt; 102 }
&lt;/c&gt;&lt;P&gt;


&lt;!-- Node text goes above. Div tags should contain sig only --&gt;
&lt;div class="pmsig"&gt;&lt;div class="pmsig-708738"&gt;
&lt;p&gt;Cheers Rolf
&lt;/div&gt;&lt;/div&gt;</field>
<field name="root_node">
1007684</field>
<field name="parent_node">
1007687</field>
</data>
</node>
