English 中文(简体)
• 在使用XML时,我怎么能去除古物:
原标题:How Can I Remove Magic Number when using XML::Simple?

I did a exercise like this, how do I calculate the # of XML elements collapsed into an array by XML::Simple so I don t have to hard-code the # of elements? I plan to use the code to parse a bigger xml file. I don t want to cout the elements by manual.

请允许我用一些数字取代神学院的号码,如<条码>人。 我知道,我可以方便地利用C#中的这种发言。

#!/usr/bin/perl -w
use strict;
use XML::Simple;
use Data::Dumper;

my $tree = XMLin( ./t1.xml );

print Dumper($tree);
print "
";
for (my $i = 0; $i < 2; $i++) # magic number  2 
{
    print "$tree->{person}->[$i]->{first_name} $tree->{person}->[$i]->{last_name}
";
    print "
";
    for (my $j = 0; $j < 3; $j++) # magic number  3 
    {
        print $tree->{person}->[$i]->{hobbie}->[$j], "
";
    }
    print "
";
}

概述:

could not find ParserDetails.ini in C:/Perl/site/lib/XML/SAX
$VAR1 = {
           person  => [
                      {
                         hobbie  => [
                                     bungy jumping ,
                                     sky diving ,
                                     knitting 
                                  ],
                         last_name  =>  Bloggs ,
                         first_name  =>  Joe 
                      },
                      {
                         hobbie  => [
                                     Swim ,
                                     bike ,
                                     run 
                                  ],
                         last_name  =>  LIU ,
                         first_name  =>  Jack 
                      }
                    ]
        };

Joe Bloggs

bungy jumping
sky diving
knitting

Jack LIU

Swim
bike
run

我的Xml来源档案如下:

<Document>
  <person>
    <first_name>Joe</first_name>
    <last_name>Bloggs</last_name>
    <hobbie>bungy jumping</hobbie>
    <hobbie>sky diving</hobbie>
    <hobbie>knitting</hobbie>
  </person>
  <person>
    <first_name>Jack</first_name>
    <last_name>LIU</last_name>
    <hobbie>Swim</hobbie>
    <hobbie>bike</hobbie>
    <hobbie>run</hobbie>
  </person>
</Document>
最佳回答

自XML组织以来:Simple将为你生产一个阵列,很容易计算其长度。

例如,$tree->{}是一种阵列,或是一种阵列参照(通过使用XML的“Array”办法确定: 简单即使只有一人。

  • 您可以先将其长度从一个阵列本身(使用<条码>@{><>>>>>>>>> 阵列:<条码>@{ 树->{人}

  • 然后,你利用由此形成的阵列,以粗略地评估阵列中要素的大小。 (换言之,a.lenth/a.count function in other Language translation to Perl idiom scalar(@a)scalar(<>>>功能,如果已经适用微量环境,则是可以选择的。)

    在这种情况下,数字比较操作者<>将强行规定标记范围,但如果情况并非如此,可使用<代码>scalar()。

例:

# Don t forget ForceArray option of XML::Simple to ensure person and hobbie are array refs
for (my $i = 0; $i < scalar( @{ $tree->{person} } ); $i++) { # scalar() is optional here
    print "$tree->{person}->[$i]->{first_name} $tree->{person}->[$i]->{last_name}
";
    print "
";
    for (my $j = 0; $j < @{ $tree->{person}->[$i]->{hobbie} }; $j++) {
        print $tree->{person}->[$i]->{hobbie}->[$j], "
";
    }
    print "
";
}

作为说明,计算每一阵列长度的方法有点不同:#a Construction, 回归阵列最后部分的索引,例如,比阵列中元素的数量少1。 我不知道使用这两种方法之间的任何业绩差异,因此,如果你认为这两种方法都同样可读,则酌情加以使用(例如,如果你需要获得最后要素的索引,则使用<代码>#a;如果内容有点,则使用@ascalar(@a)视需要)。

http://perldoc.perl.org/perldsc.html Perl Data Structures Cookbook @perldoc

问题回答
for my $person (@{ $tree->{person} }) {
    print "$person->{first_name} $person->{last_name}

";
    for my $hobby (@{ $person->{hobbie} }) {
      print $hobby, "
";
    }
    print "
";
}

如DVK所说,确保你有<代码>。 Force Array => [qw/Person Hobby/] in their XMLin options or othersarticles得奖,条件是只有一人或任何人只有一人。

如果你使用C类肥料,你只需要知道阵列中的物品数量。 相反,你可以使用更贴现的版本:,以支付我每美元(@list )

#!/usr/bin/perl

use strict;
use warnings;

use XML::Simple qw(:strict XMLin);
use Data::Dumper;

my $tree = XMLin( ./t1.xml , KeyAttr => { }, ForceArray => [  person ,  hobbie  ]);

foreach my $person ( @{ $tree->{person} } ) {
    print "$person->{first_name} $person->{last_name}
";
    foreach my $hobbie ( @{ $person->{hobbie} } ) {
        print "$hobbie
";
    }
}

为了更安全(而且可以说更可读),你不妨检查<条码>和>;人与人;有任何<条码>和>;hobbie>各项内容,然后设法通过:

foreach my $person ( @{ $tree->{person} } ) {
    print "$person->{first_name} $person->{last_name}
";
    if(my $hobbies = $person->{hobbie}) {
        foreach my $hobbie ( @$hobbies ) {
            print "$hobbie
";
        }
    }
}




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签