Help needed with Hash

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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 (@->[0]->}) {
   my $lbldate = $_->; 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
<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?

Re: Help needed with Hash

Page wrote:
Quoted text here. Click to load it

Access each element of the hash.  $_ is the element of the
$struct->->[0]-> 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.

Site Timeline