Managing data after XML::Simple...count elements
inblosam
created: 2004-07-02 11:47:45
I am trying to get this XML parsing stuff figured out. I am using XML::Simple which has turned out to be...simple. My problem now is with handling the data afterwards. I need to know how many sets of <Table> data are in there, so I can cycle through them. I can't figure out how to get a count though!
#!/usr/bin/perl -w
use strict;

use XML::Simple;
  
my $xml = qq|

  ProvoUT84601801M
ProvoUT84605801M
ProvoSD57774605C
ProvoKY42267502E
|; my $perl = XMLin($xml); use Data::Dumper; my @table = $perl->{Table}; my @newtable = $table[0]; my $count = @newtable; print Dumper(\@newtable); print "Count is $count\n"; print "State is $newtable[0][3]{STATE}\n";
Of course I will be putting in some dynamic XML when I get this to work, so I need to know how many elements were returned so I can get city, state, and zip information out for each entry. Here are the results of the above code, and obviously the count is 1 because that is how many arrays there are. I am sure it is not overly complicated, I have just tried and tried to no avail.
$VAR1 = [
          [
            {
              'STATE' => 'UT',
              'ZIP' => '84601',
              'AREA_CODE' => '801',
              'TIME_ZONE' => 'M',
              'CITY' => 'Provo'
            },
            {
              'STATE' => 'UT',
              'ZIP' => '84605',
              'AREA_CODE' => '801',
              'TIME_ZONE' => 'M',
              'CITY' => 'Provo'
            },
            {
              'STATE' => 'SD',
              'ZIP' => '57774',
              'AREA_CODE' => '605',
              'TIME_ZONE' => 'C',
              'CITY' => 'Provo'
            },
            {
              'STATE' => 'KY',
              'ZIP' => '42267',
              'AREA_CODE' => '502',
              'TIME_ZONE' => 'E',
              'CITY' => 'Provo'
            }
          ]
        ];
Count is 1
State is KY
Re: Managing data after XML::Simple...count elements
created: 2004-07-02 12:07:05
You have:
my @table = $perl->{Table};
my @newtable = $table[0];
my $count = @newtable;
But $perl->{Table} returns an array reference, not an array. Instead, do:
my @table = @{ $perl->{Table} };
my $count = @table;
Now $count should be accurate, and @table should hold hash references (I think).
_____________________________________________________
Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;
Re^2: Managing data after XML::Simple...count elements
created: 2004-07-02 12:30:32
Thanks!! That was what I needed!! I don't think I would have been able to figure that out on my own. :)
Re: Managing data after XML::Simple...count elements
created: 2004-07-05 02:20:53
my $perl = XMLin($xml);

Calling XMLin() without specifying any options is often a bug waiting to happen, see [id://218480|this node].

Also, when looking for meaningful variable names, it might help to bear in mind that XMLin will throw away the top-level XML element name by default - why not use that:

  my $NewDataSet = XMLin($xml, KeyAttr => [], ForceArray => ['Table']);

Then when you index into the data, the Perl symbols and the XML element names correspond naturally:

  my $table = $NewDataSet->{Table};
  my $table_count = @$table;
Re^2: Managing data after XML::Simple...count elements
created: 2004-07-06 17:59:28
Fantastic. Funny enough I just ran into the problem that when there was only one result returned, it would give me an error because there was no array. Now with these options it works all around. WOW! THANKS!

perlmonks.org content © perlmonks.org and grantm, inblosam, japhy

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03