Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Perl DSC and or XML::Simple

by dataDrone (Acolyte)
on Aug 31, 2002 at 20:58 UTC ( [id://194387]=perlquestion: print w/replies, xml ) Need Help??

dataDrone has asked for the wisdom of the Perl Monks concerning the following question:

Hey Folks, A quick question if you will... I'm parsing an XML Document into a perl data structure using XML::Simple with the default (Read: No) options. I'm trying to do something with each of the "Map" elements that get parsed out of the XML but i'm not iterating correctly over the structure. Here's my code
my $refdata = XMLin('C:/myxmltest.xml'); print ref($refdata) . "\n"; # Prints out HASH print ref($refdata->{'Map'}) ."\n"; # Prints out ARRAY print ref($refdata->{'Map'}[0]) . "\n"; # Prints out HASH # But when I try ... foreach my $href ( $refdata->{'Map'} ){ print $href->{'MapId'} ."\n"; }
I get a Argument "VC3NEDX$" isn't numeric in hash element at E:\code\xmltest2.pl line 23. Bad index while coercing array into hash at E:\code\xmltest2.pl line 23. Although 'VC3NEDX$' is exactly what i want to print out and is indeed available in $refdata->{'Map'}[0]->{'MapId'} Me thinks i need a little Smack, Up-side the head. Thanks, Dan.

Replies are listed 'Best First'.
Re: Perl DSC and or XML::Simple
by Mr. Muskrat (Canon) on Aug 31, 2002 at 21:03 UTC
    I haven't tested this but it should work.
    my $refdata = XMLin('C:/myxmltest.xml'); foreach my $href ( @$refdata->{'Map'} ){ print $href->{'MapId'} ."\n"; }

    Update: I should explain. You forgot the @. $refdata->{'Map'} is an array reference so you make it @$refdata->{'Map'} and you can act on all of the values that the array reference references.

    2nd Update: I forgot the curly braces. Doh!

      I Had tried that previously and got a different error, something about "Not an ARRAYin line xxxx" I forgot about using those Dang {} curly braces.. The correct line reads....  foreach my $href ( @{$refdata->{'Map'} } ) { Thanks for your help else i probably wouldn't have tried it that way again, Dan.
Re: Perl DSC and or XML::Simple
by grantm (Parson) on Sep 01, 2002 at 23:56 UTC

    Try:

    foreach my $href ( @{$refdata->{'Map'}} ){ print $href->{'MapId'} ."\n"; }

    $refdata->{Map} is an arrayref, to get the array contents you need to stick an '@' on the front and add braces to specify what the '@' applies to. Without the braces it would try to treat $refdata as an arrayref - which it is not.

    I'm parsing an XML Document into a perl data structure using XML::Simple with the default (Read: No) options

    I can't recommend that :-) You should at least use:

    my $refdata = XMLin('C:/myxmltest.xml', forcearray => [ 'Map' ]);

    That way your code won't break if there's only one 'Map' element.

      Thanks for the 'heads up' Grant, I wouldn't have noticed for at least a few days. Always nice to get help from the module's Author, that's what i like about the Perl community.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://194387]
Approved by Mr. Muskrat
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-24 04:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found