免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2728 | 回复: 7
打印 上一主题 下一主题

求问两个关于字符串的问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-12 20:18 |只看该作者 |倒序浏览
1。怎样判断一个字符串是否为空?
   
    我用 $a =~""; 来判断,但有时会出错,有没有其他方法?

2。字符串长度有无限制,如果有,长度是多少?

   如果我需要使用的字符串很长,又不想用数组该怎么办?

3。怎样获得字符串的长度?

[ 本帖最后由 qaszxc 于 2006-3-12 20:55 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2006-03-12 21:14 |只看该作者
[quote]原帖由 qaszxc 于 2006-3-12 20:18 发表
1。怎样判断一个字符串是否为空?

unless ($string){ do something; }
  

2。字符串长度有无限制,如果有,长度是多少?

长度没有限制吧?取决于你的内存大小。


3。判断字符串长度的函数是: length()

论坛徽章:
0
3 [报告]
发表于 2006-03-13 15:46 |只看该作者
1。怎样判断一个字符串是否为空?

if (defined $aa ) {
  print "It isn't null." , "\n" ;
}else {
  print "It is null." , "\n" ;
}

如果 $aa = 0 , 上面的代码和 2楼的结果 不一样

论坛徽章:
0
4 [报告]
发表于 2006-03-13 17:02 |只看该作者
原帖由 dirac_teng 于 2006-3-13 15:46 发表
1。怎样判断一个字符串是否为空?

if (defined $aa ) {
  print "It isn't null." , "\n" ;
}else {
  print "It is null." , "\n" ;
}

如果 $aa = 0 , 上面 ...


define表示有没有定义!

和空不空是俩码子事!

论坛徽章:
0
5 [报告]
发表于 2006-03-13 17:18 |只看该作者
应该使用defined

论坛徽章:
0
6 [报告]
发表于 2006-03-13 17:39 |只看该作者
原帖由 兰花仙子 于 2006-3-12 21:14 发表
[quote]原帖由 qaszxc 于 2006-3-12 20:18 发表
1。怎样判断一个字符串是否为空?

unless ($string){ do something; }
  



Hallo,

The first question seems too simple but really not simple to answer!
It's depend on which context of the given string.
For examples:
my $string;           # undefined variable, but with my() localized.
my $string = "";    # defined variable but it's empty string.
my $string = "  ";    # defined variable but it contains space(s) or tabs.
my $string = 0;    # strange??? is boolean false? or exactly integer 0? perl treated it as undef.
my $string = "0"; # defined string with character 0 (zero). perl returns it as undef.
undef $string;     # really explicitly as undefined string.

if you don't pay attenction to the context of the given string, you will get an unexpected
result.

For better undstanding to try this:
perl -Mstrict -MData::Dumper -le 'my $s=""; print Dumper($s) unless $s'
perl -Mstrict -MData::Dumper -le 'my $s=""; print Dumper($s) unless defined $s'
perl -Mstrict -MData::Dumper -le 'my $s; print Dumper($s)'
perl -Mstrict -MData::Dumper -le 'my $s="0"; if ($s){print Dumper($s)}'
perl -Mstrict -MData::Dumper -le 'my $s="0"; if ($s ne ""){print Dumper($s)}'
perl -Mstrict -le 'my $s=0; if (defined $s){print "string: >$s<"}'
... more ...
....testing by yourself.


Best, ulmer

---------------------------
Just 4 Fun.

论坛徽章:
0
7 [报告]
发表于 2006-03-13 18:00 |只看该作者

根据楼上的建议,测试了一下

4楼说的对,我误解了 $s=""。  

另外楼上的建议很好,我测试了一下
...
my $string = 0;    # strange??? is boolean false? or exactly integer 0? perl treated it as undef.
...
这句不对。if ( $string ) 是返回假的,但 if (defined $string)是返回真的,所以说 “perl treated it as undef”是不对的。


...
my $string = "0"; # defined string with character 0 (zero). perl returns it as undef.
...
楼上的既然说 是 “defined string”,为何又说“perl returns it as undef”? 针对这句:if ( $string ) 是返回假的,但 if (defined $string)是返回真的。

[ 本帖最后由 dirac_teng 于 2006-3-13 18:06 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2006-03-13 18:10 |只看该作者
原帖由 dirac_teng 于 2006-3-13 18:00 发表
4楼说的对,我误解了 $s=""。  

另外楼上的建议很好,我测试了一下
...
my $string = 0;    # strange??? is boolean false? or exactly integer 0? perl treated it as undef.
...
这句不对。i ...



You are right!
I just want somebody to test and find what's happend.
Returned defined, undefined or boolean (true/false) strongly depended on CONTEXT
and it's control condition!


Bets, ulmer
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP