Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Help with attributes and XML::Simple

by Athanasius (Archbishop)
on Aug 01, 2012 at 09:08 UTC ( [id://984737]=note: print w/replies, xml ) Need Help??


in reply to Help with attributes and XML::Simple

Building on the correction by Anonymous Monk above, the following code gives you a hash in which each entry has a vendorUniqueKey as the key and the corresponding name as the value:

#! perl use strict; use warnings; use Data::Dumper; use XML::Simple; my $xmldoc = XMLin("dsys.xml"); my $vendors = $xmldoc->{'Catalog'}->{'Vendors'}; my %vendors = %{ $vendors->{'Vendor'} }; my %unique_vendors; while (my ($outer_key, $outer_value) = each %vendors) { if (ref($outer_value) eq 'HASH') { my ($inner_key, $inner_value) = each %$outer_value; if ($inner_key eq 'vendorUniqueKey') { $unique_vendors{$inner_value} = $outer_key; } else { warn "Inner key is not 'vendorUniqueKey'"; } } else { warn "Outer value is not a hash reference"; } } print Dumper(\%unique_vendors);

Output:

$VAR1 = { 'BETASYS' => 'Beta Systems Software', 'BMC' => 'BMC Software', 'ALLENGRP' => 'Allen Systems', 'AONIX' => 'Aonix North America' };

HTH,

Athanasius <°(((><contra mundum

Replies are listed 'Best First'.
Re^2: Help with attributes and XML::Simple
by Anonymous Monk on Aug 01, 2012 at 15:55 UTC

    If you want to use XML::Simple, then this is also one possible solution

    my %resulting_vendors; my %vendors = %{ $xmldoc->{Catalog}{Vendors}{Vendor} }; foreach my $vendor2 (keys %vendors ) { print "vendorUniqueKey: [", $vendors{$vendor2}{vendorUniqueKey}, " +], vendor2: [$vendor2]\n"; $resulting_vendors{$vendors{$vendor2}{vendorUniqueKey}}=$vendor2; } print Data::Dumper->Dump( [\%resulting_vendors], [qw(resulting_vendors +)] );

    The advantage of this code is, that it only uses things that you also used

    The line

    my %vendors = %{ $xmldoc->{Catalog}{Vendors}{Vendor} };

    isn't really necessary, you can freely use the right part instead of %vendors, if you really want.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://984737]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2025-06-24 15:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.