Help needed with Hash

P

Page

I'm sure this has an easy answer, but I'm still learning.

I have the following code...

use XML::Simple;
my $simple = XML::Simple->new();
my $struct = $simple->XMLin("$map", forcearray => 1, keeproot => 1);

for (@{$struct->{treemap}->[0]->{label}}) {
my $lbldate = $_->{date}; if (ref($lbldate) eq "HASH") { $lbldate =
""; }
print '<div unselectable="on" class="lbl">'."\n";
print $_."\n"; # this is the line with the problem
print "</div>\n";
}

which reads the following XML file (code snipped to show relavent
XML):
<label date="09/28/2004">CONTRIBUTION</label>
<label date="09/28/2004">COST</label>
<label date="09/28/2004">MILES</label>


Everything seems to work fine, but instead of getting the words
"CONTRIBUTION", "COST", or "MILES", I get "HASH(0x1c282ec)". This
same code works fine for some other XML, so I'm confused. How do I
fix the line above to display the contents of the label object and not
the HASH?
 
J

J. Gleixner

Page said:
I'm sure this has an easy answer, but I'm still learning.

I have the following code...

use XML::Simple;
my $simple = XML::Simple->new();
my $struct = $simple->XMLin("$map", forcearray => 1, keeproot => 1);

for (@{$struct->{treemap}->[0]->{label}}) {
my $lbldate = $_->{date}; if (ref($lbldate) eq "HASH") { $lbldate =
""; }
print '<div unselectable="on" class="lbl">'."\n";
print $_."\n"; # this is the line with the problem
print "</div>\n";
}

which reads the following XML file (code snipped to show relavent
XML):
<label date="09/28/2004">CONTRIBUTION</label>
<label date="09/28/2004">COST</label>
<label date="09/28/2004">MILES</label>


Everything seems to work fine, but instead of getting the words
"CONTRIBUTION", "COST", or "MILES", I get "HASH(0x1c282ec)". This
same code works fine for some other XML, so I'm confused. How do I
fix the line above to display the contents of the label object and not
the HASH?

Access each element of the hash. $_ is the element of the
$struct->{treemap}->[0]->{label} array, which seem to contain a hash, so
then you go through each key of the hash.

Give Data::Dumper a try, to display how the data is stored, and to help
you diagnose how you can access the hash in the future.
use XML::Simple; use Data::Dumper;
my $simple = XML::Simple->new();
my $struct = $simple->XMLin("$map", forcearray => 1, keeproot => 1);
print Dumper($struct);

You could also call Dumper in your foreach, to see the structure of $_,
to help you print the values you need.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top