Perl 哈希键仅在紧随其后的换行符时打印
也许我很愚蠢,但这对我来说没有意义......我有一个通过从大量文件中读取唯一代码和错误代码而构建的哈希值。当我尝试打印键=值对时,除非紧接着换行符,否则键不会出现。
这是代码:
foreach my $key (keys %codehash){
print "Key: $key\tValue: $codehash{$key}\n";
print "Key: $key\n";
print "Value: $codehash{$key}\n";
print "\n\n";
}
这是输出:
Value: NoParamSpecified
Key: 016C
Value: NoParamSpecified
Value: billingAddress.firstName.lengthLong
Key: 003M
Value: billingAddress.firstName.lengthLong
Value: billingAddress.address1.lengthLong
Key: 0041
Value: billingAddress.address1.lengthLong
请注意,它甚至没有打印第一个语句中的“Key:”纯文本,只是打印制表符及其他内容。我以前从未遇到过这种情况。
Maybe I am being stupid, but this isn't making sense to me... I have a hash built from reading unique codes and error codes from numerous files. When I try to print out the key=value pairs, the keys are not appearing unless immediately followed by a newline.
Here's the code:
foreach my $key (keys %codehash){
print "Key: $key\tValue: $codehash{$key}\n";
print "Key: $key\n";
print "Value: $codehash{$key}\n";
print "\n\n";
}
Here's the output:
Value: NoParamSpecified
Key: 016C
Value: NoParamSpecified
Value: billingAddress.firstName.lengthLong
Key: 003M
Value: billingAddress.firstName.lengthLong
Value: billingAddress.address1.lengthLong
Key: 0041
Value: billingAddress.address1.lengthLong
Notice that it is not even printing the "Key: " plain text from the first statement, just the tab and beyond. I have never come across this before.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该行为与末尾包含
"\r"
的所有键一致。十六进制转储将证实这一点。确认这一点的另一种方法是在打印之前对键运行合适的正则表达式替换:
The behavior is consistent with all the keys containing a
"\r"
at the end. The hex dump will confirm this.Another way to confirm this would be to run a suitable regex substitution over the keys before printing it out:
我敢打赌,因为您正在读取的数据中的某处包含格式化字符,并且您将它们包含在关键数据中(如果格式化字符正在执行诸如“返回到开头的操作”之类的操作,那么这很糟糕线”等)。
为了解决这个问题,请尝试以 base64 或十六进制打印密钥,我敢打赌您会发现它的字符串比您想象的要长得多。
I'm going to bet that because you're reading in data that somewhere the data contains formatting characters and you're including them in key data (which is bad if the formatting characters are doing things like "go back to the beginning of the line", etc).
To get around this, try printing the key in base64 or hex and I bet you'll see it's a much longer string than you thought it was.
这是终端 I/O 的一部分。它只打印在行尾。设置输出自动刷新以使其在每个打印语句后打印:
请参阅 perldoc perlvar 并搜索 /OUTPUT_AUTOFLUSH/。
This is part of the terminal I/O. It only prints at the end of a line. Set the output autoflush to get it to print after every print statement:
See
perldoc perlvar
and search for /OUTPUT_AUTOFLUSH/.