Chinaunix

标题: foreach $_(@d) [打印本页]

作者: drosophilia    时间: 2009-06-01 01:46
标题: foreach $_(@d)
foreach $_(@d)里面的$_(@d)是个什么意思,怎么得到逻辑值!请解答下!

好久都没用Perl了!
作者: wxlfh    时间: 2009-06-01 08:08
我看你是根本就没用过 Perl ,骗人也要打草稿啊。

$_就是 @d数组中的元素,foreach提供的是列表环境,首先把 @d展开成一个列表,然后把$_设置成第一个元素的别名,所以你修改$_就是修改@d中的元素。循环直到最后一个元素处理完后结束。

[ 本帖最后由 wxlfh 于 2009-6-1 08:11 编辑 ]
作者: Perl_Er    时间: 2009-06-01 08:30
$_

The default input and pattern-searching space. The following pairs are equivalent:

    while (<>) {...}        # equivalent only in while!
    while (defined($_ = <>)) {...}

    /^Subject:/
    $_ =~ /^Subject:/

    tr/a-z/A-Z/
    $_ =~ tr/a-z/A-Z/

    chomp
    chomp($_)

Here are the places where Perl will assume $_ even if you don't use it:

    *

      Various unary functions, including functions like ord() and int(), as well as the all file tests (-f , -d ) except for -t , which defaults to STDIN.
    *

      Various list functions like print() and unlink().
    *

      The pattern matching operations m//, s///, and tr/// when used without an =~ operator.
    *

      The default iterator variable in a foreach loop if no other variable is supplied.
    *

      The implicit iterator variable in the grep() and map() functions.
    *

      The default place to put an input record when a <FH> operation's result is tested by itself as the sole criterion of a while test. Outside a while test, this will not happen.

As $_ is a global variable, this may lead in some cases to unwanted side-effects. As of perl 5.9.1, you can now use a lexical version of $_ by declaring it in a file or in a block with my. Moreover, declaring our $_ restores the global $_ in the current scope.

(Mnemonic: underline is understood in certain operations.)
作者: MMMIX    时间: 2009-06-01 09:13
原帖由 drosophilia 于 2009-6-1 01:46 发表
foreach $_(@d)里面的$_(@d)是个什么意思,怎么得到逻辑值!请解答下!

好久都没用Perl了!

既然如此,那把当初学时用的资料翻出来看看不就结了?即使资料找不到,那么这类语法问题查查 perlsyn 总应该会吧?
作者: drosophilia    时间: 2009-06-01 10:55
原帖由 wxlfh 于 2009-6-1 08:08 发表
我看你是根本就没用过 Perl ,骗人也要打草稿啊。

$_就是 @d数组中的元素,foreach提供的是列表环境,首先把 @d展开成一个列表,然后把$_设置成第一个元素的别名,所以你修改$_就是修改@d中的元素。循环直到 ...



你还真猜错了,我真的是一年多没用了!哈哈,真衰,没关系,还是谢谢你!
然后补充一个问题,就是查看帮助文档的问题,就是那个perldoc工具
查某个函数我知道,例如perldoc -f print
如果我想查特殊的变量了,例如$_, $*等,我该怎么写
另外就是怎么查看语法

贴个例子最好!
作者: xiaoyu9805119    时间: 2009-06-01 11:28
原帖由 MMMIX 于 2009-6-1 09:13 发表

既然如此,那把当初学时用的资料翻出来看看不就结了?即使资料找不到,那么这类语法问题查查 perlsyn 总应该会吧?


是不是今天六一,楼主来忽悠大家了?
作者: Perl_Er    时间: 2009-06-01 11:34
标题: 回复 #5 drosophilia 的帖子
你先去google,百度找找吧,如果找不到再来这里
作者: MMMIX    时间: 2009-06-01 11:47
原帖由 drosophilia 于 2009-6-1 10:55 发表


如果我想查特殊的变量了,例如$_, $*等,我该怎么写

Perl 的内置变量在 perlvar 中描述。
另外就是怎么查看语法

不是说了么?看 perlsyn




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2