I managed to come up with the following code that is not quite groundbreaking, but which you may find useful. The '..' expression is false until first_condition is true (until $i == 3). It continues to be true until second_condition is true (the next number which is 3 mod 10).
Playing around with the *_condition functions will help get the hang of things. It helped a bit for me -- I still am not entirely comfortable with the operator (at least in scalar context) myself, but I'm getting there.
In Abigail-II's code, 32..32 is syntactic sugar for $.==32 .. $.==32. So when $. is equal to 32, the expression becomes true, and then becomes false for all subsequent evaluations. The unless reverses the logic so that each line of input is printed only if it's not #32. I like it!
use strict;
for my $i (0 .. 20) {
if (first_condition($i) .. second_condition($i)) {
print "true for $i\n";
} else {
print "false for $i\n";
}
}
sub first_condition {
return $_[0] == 5;
}
sub second_condition {
return $_[0] % 10 == 3;
}
__END__
false for 0
false for 1
false for 2
false for 3
false for 4
true for 5
true for 6
true for 7
true for 8
true for 9
true for 10
true for 11
true for 12
true for 13
false for 14
false for 15
false for 16
false for 17
false for 18
false for 19
false for 20
blokhead
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.