关于一个perl编程中的哈希表打印的奇怪问题
时间:2011-04-21
来源:互联网
例如下边的程序:#!/usr/bin/perl
%hash=("A"=>0,"C"=>0,"G"=>0,"U"=>0);
print %hash;
print "%hash";
上边的程序中,第一个print语句可以打印出哈希表的键/值,第二个print语句打印的结果却是%hash,不知道其中的原因,求高人指点。
#!/usr/bin/perl
$a=1;
print $a;
print "$a";
这两个print语句都可以打印出$a的值,但是上边的哈希表的print语句却不可以加引号。
求高人指点
%hash=("A"=>0,"C"=>0,"G"=>0,"U"=>0);
print %hash;
print "%hash";
上边的程序中,第一个print语句可以打印出哈希表的键/值,第二个print语句打印的结果却是%hash,不知道其中的原因,求高人指点。
#!/usr/bin/perl
$a=1;
print $a;
print "$a";
这两个print语句都可以打印出$a的值,但是上边的哈希表的print语句却不可以加引号。
求高人指点
作者: xdp719 发布时间: 2011-04-21
只有 $ 或 @ 开头的才能内插
作者: zhlong8 发布时间: 2011-04-21
你好,本人初学perl,请问内插是什么意思
作者: xdp719 发布时间: 2011-04-21
就是这个 "$a" 双引号中会插入取变量的值
作者: zhlong8 发布时间: 2011-04-21
回复 xdp719
QUOTE:
Perl's Interpolation Syntax
When Perl encounters a string that can be interpolated, it scans it for three significant characters, $, @
and \. If any of these are present and not escaped (prefixed with a backslash) they trigger interpolation
of the text immediately following. What actually happens depends on the character:
Character Action
\ Interpolate a metacharacter or character code
$ Interpolate a scalar variable or evaluate an expression in scalar context
@ Interpolate an array variable or evaluate an expression in list context
If a string does not contain any of these then there is nothing to interpolate and Perl will use the string
as it is. Furthermore, Perl first checks for strings that can be interpolated at compile-time, weeding out
all those that are either already constant and do not require interpolation, or can be interpolated to a
constant value. Consequently it does not matter much if we use double quotes for our constant strings or
not; Perl will detect and optimize them before execution starts.
When Perl encounters a string that can be interpolated, it scans it for three significant characters, $, @
and \. If any of these are present and not escaped (prefixed with a backslash) they trigger interpolation
of the text immediately following. What actually happens depends on the character:
Character Action
\ Interpolate a metacharacter or character code
$ Interpolate a scalar variable or evaluate an expression in scalar context
@ Interpolate an array variable or evaluate an expression in list context
If a string does not contain any of these then there is nothing to interpolate and Perl will use the string
as it is. Furthermore, Perl first checks for strings that can be interpolated at compile-time, weeding out
all those that are either already constant and do not require interpolation, or can be interpolated to a
constant value. Consequently it does not matter much if we use double quotes for our constant strings or
not; Perl will detect and optimize them before execution starts.
作者: Perl_Er 发布时间: 2011-04-21
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28