作者liyih ()
看板Perl
标题Re: [问题] 串列问题
时间Fri Mar 18 09:26:15 2011
# perldoc perlsyn
Compound Statements
The following compound statements may be used to control flow:
if (EXPR) BLOCK
if (EXPR) BLOCK else BLOCK
if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK
LABEL while (EXPR) BLOCK
LABEL while (EXPR) BLOCK continue BLOCK
LABEL until (EXPR) BLOCK
LABEL until (EXPR) BLOCK continue BLOCK
LABEL for (EXPR; EXPR; EXPR) BLOCK
LABEL foreach VAR (LIST) BLOCK
LABEL foreach VAR (LIST) BLOCK continue BLOCK
LABEL BLOCK continue BLOCK
※ 引述《liyih ()》之铭言:
: ※ 引述《StarTouching (抚星)》之铭言:
: : 我是Perl新手 有程式基础 对串列有个小问题....
: : 串列给定可以这样写:
: : @ary = (1,2,3);
: : 或
: : @ary = (1..10);
: : 或
: : @ary = @seq;
: : 而foreach这样用:
: : foreach(1,2,3){}
: : 或
: : foreach(1..10){}
: : 或
: : foreach(@seq){}
: : 这样有个小问题是 (1,2,3)才能代表串列 那为什麽foreach没有两层括弧?
: : 如果串列的括弧可以被省略 那为什麽 @ary = 1,2,3; 这样不行?
: : 我知道Perl是个方便为上的语言,
: : 但为求方便记忆, 想说还是问问看。
: : 请问我该怎麽理解呢?
: # perldoc perlsyn
: Statement Modifiers
: ...略...
: if EXPR
: unless EXPR
: while EXPR
: until EXPR
: foreach LIST
: ^^^^
: ...略...
: The "foreach" modifier is an iterator: it executes the statement once
: for each item in the LIST (with $_ aliased to each item in turn).
: print "Hello $_!\n" foreach qw(world Dolly nurse);
: # perldoc perlop
: 1. Comma Operator 「,」
: ...略...
: In list context, it’s just the list argument separator, and inserts
: ^^^^^^^^^^^^^^^^^^^^^^^
: both its arguments into the list. These arguments are also evaluated
: from left to right.
: ...略...
: 2. Terms and List Operators
: ...略...
: A TERM has the highest precedence in Perl. They include variables,
: quote and quote-like operators, any expression in parentheses, and any
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
: function whose arguments are parenthesized. Actually, there aren’t
: ^^^^^^^^^^^^^ ^^^^^^^^^^^^^
: really functions in this sense, just list operators and unary operators
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
: behaving as functions because you put parentheses around the arguments.
: These are all documented in perlfunc.
: ...略...
: 3. Range Operators 「..」
: Binary ".." is the range operator, which is really two different
: operators depending on the context. In list context, it returns a list
: of values counting (up by ones) from the left value to the right value.
: ...略...
: 大致上摘录 perldoc 的内容,应该可以发现其中的差异。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.114.64.130