Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
@array[0 ..0] is one and not zero elements!

DB<103> @array=a..f => ("a", "b", "c", "d", "e", "f") DB<104> @array[0..0] => "a"

UPDATE:

your statistical proof falls into the trap

scalar @array != scalar (LIST)

if your function() returns a one element list (0), then scalar function() will be 0, since 0 is the last element of the list.

(more detailed in Re^2: Idiom to return 0 or random number of array elements by eyepopslikeamosquito )

initializing @array = 1..10 makes it more obvious, b/c the last element of a sublist is now identical to the length!

DB<106> @array = 1 .. 10; => (1, 2, 3, 4, 5, 6, 7, 8, 9, 10) DB<107> sub x{ return @array[ 0 .. int( rand @array ) ]; }; => 0 DB<108> $stats{ scalar( x() ) }++ for 1 .. 1000; => "" DB<109> \%stats; => { 1 => 100, 2 => 101, 3 => 108, 4 => 108, 5 => 101, 6 => 103, 7 => + 79, 8 => 98, 9 => 100, 10 => 102 }

Cheers Rolf


In reply to Re^2: Idiom to return 0 or random number of array elements by LanX
in thread Idiom to return 0 or random number of array elements by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (15)
    As of 2014-09-22 16:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (198 votes), past polls