在 Perl 中测试不同类型的哈希值?
我正在编写一个小型 Perl 脚本,它通过 XML::Simple
遍历 XML 文件。
my $xml = new XML::Simple;
my $detail= $xml->XMLin($xml_local);
有时,XML 中元素的内容是空的。
当 XML 中的元素中没有内容时,我尝试使用以下方法打印内容:
print $detail->{Parsing}->{Through}->{XML}->{ElementContents}
我得到输出:
HASH(0x18948c4)
......或类似的东西...... ..唯一的区别是 () 之间的字符
我想测试内容是否为空并将变量默认为其他内容 - 也许 ''
或 ""
- 除了哈希引用/地址/无论是什么之外的任何内容。
我尝试了这个,但收到一个错误,它不是数组引用:
print $detail->{Parsing}->{Through}->{XML}->{ElementContents}[0]
UPDATE
使用 Data::Dumper
输出其中一个元素:
'something' => [
{
'somedetail' => '',
'somedetail' => '',
'somedetail' => 'http://www.google.com'
'somedetail' => 'google',
'somedetail' => '1',
'somedetail' => '01/21/02'
},
如何使用 Perl 测试这些 ''
空字符串?除非启用了某些过滤,否则它们将以 HASH(0x18948c4)
形式返回。
I'm writing a small Perl script that goes through an XML file via XML::Simple
my $xml = new XML::Simple;
my $detail= $xml->XMLin($xml_local);
Sometimes, the contents of an element in the XML are empty.
When there is no content in an element in the XML, and I try to print out the contents using:
print $detail->{Parsing}->{Through}->{XML}->{ElementContents}
I get the output:
HASH(0x18948c4)
......or something similar..... the only difference is the chars between the ()'s
I want to test if the content is empty and default the variable to something else - maybe ''
or ""
- anything but the hash reference/address/whatever that is.
I tried this, but got an error that its not an array reference:
print $detail->{Parsing}->{Through}->{XML}->{ElementContents}[0]
UPDATE
Output of one of the elements using Data::Dumper
:
'something' => [
{
'somedetail' => '',
'somedetail' => '',
'somedetail' => 'http://www.google.com'
'somedetail' => 'google',
'somedetail' => '1',
'somedetail' => '01/21/02'
},
How can I test for these ''
empty strings using Perl? They are returned as HASH(0x18948c4)
unless some filtering is enabled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它打印
HASH(0x18948c4)
的原因是因为该值的内容实际上不是空的,而是一个 hashref。当您打印某些内容时,Perl 会尝试对该内容进行字符串化,哈希引用的字符串化结果是 HASH(address),其中地址是实际哈希的地址。打印该哈希引用的实际内容,如下所示:
如果如您所说“没有内容”,则它可能是一个空哈希引用:
如果是这样,您可以通过以下方式检查它:
第一个条件确保它是一个哈希引用,第二个条件是取消引用产生的哈希值有零个元素作为其键 - 这意味着它是一个被引用的空哈希值。
但是,根据我对 XML::Simple 的记忆,我严重怀疑它是一个空哈希 - 并且如上所示执行 Data::Dumper 打印将向您展示如何处理它。您应该始终以这种方式打印出未知的数据结构,以弄清楚如何处理它们。
例如,如果您的 Data::Dumper 输出是:
那么您需要打印
$detail->{Parsing}->{Through}->{XML}->{ElementContents}->{a显然。再次强调,要小心只打印标量而不是 arrayref 或 hashref,因此根据需要深入数据结构以获得标量。
The reason it prints
HASH(0x18948c4)
is because the contents of that value are NOT in fact empty, but a hashref. When you print something, Perl tries to stringify that something, and stringified result of a hash reference isHASH(address)
where address is the address of the actual hash.Print the actual contents of that hashref as follows:
If as you say there are "no contents", it will probably be an empty hashref:
If so, you can check for it via:
First condition ensures it's a hashref, second, that the hash resulting from its dereference has zero elements as its keys - meaning it's an empty hash being referenced.
However, I seriously doubt it's an empty hash from what I recall about XML::Simple - and doing the Data::Dumper print as shown above will show you HOW to deal with it. You should always print out unknown data structures this way to figure out what to do with them.
E.g., if your Data::Dumper output was:
Then you need to print
$detail->{Parsing}->{Through}->{XML}->{ElementContents}->{a}
, obviously. Again, be careful to only print something that is a scalar and not an arrayref or hashref, so go down the data structure as much as needed to get to a scalar.这是 DVK 答案的修改版本,对我有用:
我需要删除他给我的
if(condition1 && condition2)
语句的第二个条件。This is a modified version of DVK's answer that worked for me:
I needed to remove the 2nd condition of the
if(condition1 && condition2)
statement he gave me.