免费注册 查看新帖 |

Chinaunix

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

Perl新手,有问题请教!谢谢! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-10 14:45 |只看该作者 |倒序浏览
第一次来,发现国内真正能够在perl方面答疑解惑的地方只有网络了。

希望各位不吝赐教。。。

问题如下:

@output = qw(111 222 333);

printf @output;

printf "output";

为什么2句printf输出结果不同?

前者是111

而后者是完整的@output?

谢谢。。。

论坛徽章:
0
2 [报告]
发表于 2009-09-10 15:33 |只看该作者
原帖由 ExclusivePig 于 2009-9-10 14:45 发表
第一次来,发现国内真正能够在perl方面答疑解惑的地方只有网络了。

希望各位不吝赐教。。。

问题如下:

@output = qw(111 222 333);

printf @output;

printf "output";

为什么2句printf输出结 ...


perldoc -f  printf
printf FILEHANDLE FORMAT, LIST
printf FORMAT, LIST
    Equivalent to "print FILEHANDLE sprintf(FORMAT, LIST)", except that "$\" (the output record separator) is not appended. The first argument of the list will be interpreted as the "printf" format. See "sprintf" for an explanation of the format argument. If "use locale" is in effect, the character used for the decimal point in formatted real numbers is affected by the LC_NUMERIC locale.  See perllocale.
    Don't fall into the trap of using a "printf" when a simple "print" would do.  The "print" is more efficient and less error prone.

第一个printf将@output的第一个元素作为格式化控制串, 相当于"printf  111, 222, 333",  
后面两个元素并没有在格式化控制串中作声明,所以只输出111;

第二个printf将"@output"作为格式化控制串,相当于printf "111 222 333", 故输出111 222 333

论坛徽章:
0
3 [报告]
发表于 2009-09-10 15:48 |只看该作者
原帖由 iceberg77 于 2009-9-10 15:33 发表


perldoc -f  printf
printf FILEHANDLE FORMAT, LIST
printf FORMAT, LIST
    Equivalent to "print FILEHANDLE sprintf(FORMAT, LIST)", except that "$\" (the output record separator) is not appe ...


谢谢。。。明白了,


而且我也明白了,其实一直是我自己乌龙,以前一直是C程序员,输出都是想到printf,其实实际上我改用print,那样结果就一样了。。

呵呵!

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2009-09-10 16:00 |只看该作者
原帖由 ExclusivePig 于 2009-9-10 15:48 发表


谢谢。。。明白了,


而且我也明白了,其实一直是我自己乌龙,以前一直是C程序员,输出都是想到printf,其实实际上我改用print,那样结果就一样了。。

呵呵!

用 print 还是不一样。
print @output 的输出内容是 $str = join $, , @output;
print "@output" 的输出内容是 $str = join $", @output;

$, 的缺省值是 '',$" 的缺省值是 ' ',
也就是说,
print "@output" 输出来的 111 222 333 中间有个空格,
print @output 输出来的没空格。

论坛徽章:
0
5 [报告]
发表于 2009-09-10 16:02 |只看该作者
原帖由 flw 于 2009-9-10 16:00 发表

用 print 还是不一样。
print @output 的输出内容是 $str = join $, , @output;
print "@output" 的输出内容是 $str = join $", @output;

$, 的缺省值是 '',$" 的缺省值是 ' ',
也就是说,
print "@ ...



谢谢,已经发现了。。。

论坛徽章:
0
6 [报告]
发表于 2009-09-10 16:31 |只看该作者
不好意思 问下$str 是什么?  默认的变量么

论坛徽章:
0
7 [报告]
发表于 2009-09-11 10:05 |只看该作者

回复 #6 yz86yz 的帖子

不是. 他只是举了一个例子把输出内容放到这个自己定义的变量里面。
要知道是不是特殊变量去perlvar里面查一查

论坛徽章:
0
8 [报告]
发表于 2009-09-11 13:43 |只看该作者

回复 #7 Perl_Er 的帖子

谢啦 是我多想了 呵呵
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP