2008年12月30日星期二

[PerlChina] Re: Question about if (@array)

你的这种说法不全面, 实际最终都是计算array的长度. my @array = ();  if (@array)这个也是为假的,

array为undef,最终也是长度为0.


2008/12/30 smallfish <smallfish@live.cn>
数组为空,未定义就是假把。

From: 郑海涛
Sent: Tuesday, December 30, 2008 4:57 PM
Subject: [PerlChina] Re: Question about if (@array)

1.觉的两者最终的意思是一样的, if(@array), 肯定是转成了标量来进行判断,不明白楼上说的判断真假是什么意思.
2.undef是将该变量清空,即置为未初始化. 而delete则是去除该变量的定义.即在此之后该变量不可用了.这种做法再python中也是有的.

2008/12/30 smallfish <smallfish@live.cn>

1.结果是一样的,不过意思不一样,@array是判断数组是否真假,scalar则是把数组转成变量,里面的值其实是数组的长度了。
2.你可以参考:perldoc -f undef 和perdoc -f delete,他们可以给你答案。
--
blog : http://hi.baidu.com/smallfish7788

--------------------------------------------------
From: "Ray" <chenrano2002@gmail.com>
Sent: Tuesday, December 30, 2008 4:24 PM
To: "PerlChina 论坛" <perlchina@googlegroups.com>
Subject: [PerlChina] Question about if (@array)

>
> Hi, all
>
> Would you explain the output of the following perl codes?
>
> question 1:
> Is the sentence "if (@array) " same as "if ( scalar(@array) )"??
> question 2:
> what's the difference of "undef $variable " and "delete $variable"??
>
> Thanks,
> Ray
>
>
>
> -------------------------------------------
> code----------------------------------
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my @host = ();
> my @host1 = ('fdfd', 'fdfdfd');
> my @host2 = undef;
>
> if ( @host ) {
>         print "host ok\n";
> } else {
>         print "host fail\n";
> }
>
> if ( @host1 ) {
>         print "host1 ok\n";
> } else {
>         print "host1 fail\n";
> }
>
> if ( @host2 ) {
>         print "host2 ok\n";
> } else {
>         print "host2 fail\n";
> }
> -------------------------------------------
> code----------------------------------
>
> and the output is :
> host fail
> host1 ok
> host2 ok
>
>
> >
>






--~--~---------~--~----~------------~-------~--~----~
您收到此信息是由于您订阅了 Google 论坛"PerlChina 论坛"论坛。
 要在此论坛发帖,请发电子邮件到 perlchina@googlegroups.com
 要退订此论坛,请发邮件至 perlchina+unsubscribe@googlegroups.com
 更多选项,请通过 http://groups.google.com/group/perlchina?hl=zh-CN 访问该论坛

-~----------~----~----~----~------~----~------~--~---

没有评论: