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.@arr = (1 .. 20); foreach $i ( 0 .. $#arr ) { if ( $i = (5 .. 10) ) { print "$i\n"; } }
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |