Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How to access hash of arrays values using perl

by veerubiji (Sexton)
on Dec 15, 2011 at 17:12 UTC ( #943791=perlquestion: print w/ replies, xml ) Need Help??
veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, This is very simple for experts but not for beginners like me.I have one xml file and I processed using XML::Simple like this

my $file="service.xml"; my $xml = new XML::Simple; my $data = $xml->XMLin("$file", ForceArray =>['Service','SystemReact +ion','Customers','Suppliers','SW','HW'],); print Dupmer ($data);

it printing hash of data like this

$var1={ 'Service' => [ { 'Suppliers' => [ { 'SW' => [ { 'Path' => '/work/service.xml', 'Service' => 'b7a' }, { 'Path' => '/work/service1.xml', 'Service' => 'b7b' }, { 'Path' => '/work/service2.xml', 'Service' => 'b5' } ] } ], 'Id' => 'SKRM', 'Customers' => [ { 'SW' => [ { 'Path' => '/work/service.xml', 'Service' => 'ASOC' } ] } ], 'Des' => 'Control the current through the pipe', 'Name' => ' Control unit' }, { 'Suppliers' => [ { 'HW' => [ { 'Type' => 'W', 'Path' => '/work/hardware.xml', 'Nr' => '18', 'Service' => '1' }, { 'Type' => 'B', 'Path' => '/work/hardware.xml', 'Nr' => '7', 'Service' => '1' }, { 'Type' => 'k', 'Path' => '/work/hardware.xml', 'Nr' => '1', 'Service' => '1' } ] } ], 'Id' => 'ADTM', 'Customers' => [ { 'SW' => [ { 'Path' => '/work/service.xml', 'Service' => 'SDCR' } ] } ], 'Des' => 'It delivers actual motor speed', 'Name' => ' Motor Drivers and Diognostics' }, ] 'Systemreaction'=>[ { ------- ------- ------- } { ------- ------- ------- }, ] };

How to access each elements in the service and systemReaction(not provided). because I am using "$data" in further processing. So I need to access each Id,customers, suppliers values in each service. How to get particular value from service to do some process with that value.for example I need to get all Id values form service and create nodes for each id values.

Thanks in advance

Comment on How to access hash of arrays values using perl
Select or Download Code
Re: How to access hash of arrays values using perl
by Corion (Pope) on Dec 15, 2011 at 17:25 UTC
Re: How to access hash of arrays values using perl
by TJPride (Pilgrim) on Dec 15, 2011 at 19:56 UTC
    Not sure what you mean by "access each element", since the elements seem to be a variety of different structures, but here's how to get into the structure a couple levels. For everyone else's benefit, I added something into that hole at the bottom and then ran this through Data::Dumper first and eliminated a bunch of the whitespace at the left side to clean it up.

    use strict; use warnings; my $nested = { 'Systemreaction' => [ { 'Data' => 'x' }, { 'Data' => 'x' } ], 'Service' => [ { 'Customers' => [ { 'SW' => [ { 'Path' => '/work/servi +ce.xml', 'Service' => 'ASOC' } ] } ], 'Id' => 'SKRM', 'Name' => ' Control unit', 'Suppliers' => [ { 'SW' => [ { 'Path' => '/work/servi +ce.xml', 'Service' => 'b7a' }, { 'Path' => '/work/servi +ce1.xml', 'Service' => 'b7b' }, { 'Path' => '/work/servi +ce2.xml', 'Service' => 'b5' } ] } ], 'Des' => 'Control the current through the pipe' }, { 'Customers' => [ { 'SW' => [ { 'Path' => '/work/servi +ce.xml', 'Service' => 'SDCR' } ] } ], 'Id' => 'ADTM', 'Name' => ' Motor Drivers and Diognostics', 'Suppliers' => [ { 'HW' => [ { 'Type' => 'W', 'Path' => '/work/hardw +are.xml', 'Nr' => '18', 'Service' => '1' }, { 'Type' => 'B', 'Path' => '/work/hardw +are.xml', 'Nr' => '7', 'Service' => '1' }, { 'Type' => 'k', 'Path' => '/work/hardw +are.xml', 'Nr' => '1', 'Service' => '1' } ] } ], 'Des' => 'It delivers actual motor speed' } ] }; for my $service (@{$nested->{'Service'}}) { print "$service->{'Name'}\n"; } for my $system (@{$nested->{'Systemreaction'}}) { print "$system->{'Data'}\n"; }

      Sorry for not clearance post, I mean how to get nested elements, but I am facing difficult in getting "Type" and "Nr" values. can you help me with that, or give me some example.

      Thanks for your reply

        Using my previous definition for $nested:

        for my $service (@{$nested->{'Service'}}) { for my $suppliers (@{$service->{'Suppliers'}}) { for my $hw (@{$suppliers->{'HW'}}) { print "Type: $hw->{'Type'}\n"; print "Nr: $hw->{'Nr'}\n"; } } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (17)
As of 2014-08-01 14:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (21 votes), past polls