- 论坛徽章:
- 0
|
原帖由 MMMIX 于 2009-2-16 14:02 发表
注意看 perlop 讲解 .. 后的例子就好理解了。
后面的例子挺清楚
As a scalar operator:
if (101 .. 200) { print; } # print 2nd hundred lines, short for
# if ($. == 101 .. $. == 200) ...
next LINE if (1 .. /^$/); # skip header lines, short for
# ... if ($. == 1 .. /^$/);
# (typically in a loop labeled LINE)
s/^/> / if (/^$/ .. eof()); # quote body
# parse mail messages
while (<>) {
$in_header = 1 .. /^$/;
$in_body = /^$/ .. eof;
if ($in_header) {
# ...
} else { # in body
# ...
}
} continue {
close ARGV if eof; # reset $. each file
}
Here's a simple example to illustrate the difference between the two
range operators:
@lines = (" - Foo",
"01 - Bar",
"1 - Baz",
" - Quux");
foreach (@lines) {
if (/0/ .. /1/) {
print "$_\n";
}
} |
|