(Firstly, thanks to the monks above for more efficient routines.)

The statement: push(@deck, splice(@_,$rand,1)); is what puzzles me (in my code).

After writing a sub:

my @arr = (5 .. 10); call_sub(@arr); sub call_sub { print "\n\n@_\n\n"; for (0 .. 4) { print "$_\t@_\n"; } print "\n\n"; }

I believe that $_ is a separate variable from @_ (and you can reference a scalar in @_ with $_[$sc]).

I had always assumed that $_ and @_ were connected, and that @_ referenced the elements in the for list as an array (which it doesn't).

It's a rather new discovery for me. Looking at how useful this is (rather than having to make temporary arrays in all your subroutines to carry parameters before going into a for loop), I see why it was done that way.

Dave.


In reply to Re: Re: Unexpected value of $_ in a for loop. by David Caughell
in thread Unexpected value of $_ in a for loop. by David Caughell

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.