Okay, just so I know I got it. I'm going to go back over the previous examples and see if I know why they don't work:
@arr = (1 .. 20); foreach $i ( 0 .. $#arr ) { if ( $i == (5 .. 10) ) { print "$i\n"; } }
That one doesn't work and only outputs 0 because of the way the condition is being read. Essentially $i is working out to be '0' or false, so the first iteration of the loop is the only one that will match. Why is the expression false is the tricky part. Well, it's because the operator is never "flipped" into true mode. Both operands are being compared to $. which is at 3 when the operator is being evaluated. The left operand is not 3 so it doesn't return true or flip the operator. Next...

@arr = (1 .. 20); foreach $i ( 0 .. $#arr ) { if ( $i == 5 .. 10 ) { print "$i\n"; } }
This code doesn't work right either. It outputs 5-20 one per line. Well, first of all, because of the precedence of the assignment operator(==), the expression is being read as '($i == 5) .. 10', so the operator is getting flipped true, but never flopped false because $. is, again, 3, which is definitley not 10. NEXT!!!

@arr = (1 .. 20); foreach $i ( 0 .. $#arr ) { if ( $i = (5 .. 10) ) { print "$i\n"; } }
This one was a shot in the dark to begin with, so I'm not that upset about it. Basically this one is similar to the first one in the way that the operator is never being flipped true. So $i is being assigned a '0' or false value, which evaluates the condition in the if block to false, so no print is ever executed.

WOOHHOOOOOO!!! THanks for your help chipmunk, yer the best.

Amel - f.k.a. - kel


In reply to (One Last)Re: Range Operators Question by dsb
in thread Range Operators Question by dsb

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.