snehit.ar has asked for the wisdom of the Perl Monks concerning the following question:

Please do help me with this query ... i am getting eventid and name in diff arrays.. But Now i want out put as part of single array list : required output
$VAR1 = [ { 'eventid' => '957', 'name' => 'aaaa' }, { 'eventid' => '2667', 'name' => 'bbbb' }, { 'eventid' => '2667' 'name' => 'bbbb' }, { 'eventid' => '1503' 'name' => 'cccc' }, { 'eventid' => '1103' 'name' => 'dddd' }, { 'eventid' => '1503' 'name' => 'cccc' } ];
How can i do with multiple loop :
#!/usr/bin/env perl use strict; use warnings; use XML::XPath; use Data::Dumper; my $xml = 'ApplicationList.xml'; my $xp = XML::XPath->new(filename => $xml); my $appxpath = $xp->findnodes("//application_list/application/"); my %appid = (); foreach my $appnodeset ($appxpath->get_nodelist) { my $id = $xp->find('./@id',$appnodeset)->string_value; my $name = $xp->find('./@name',$appnodeset)->string_value; s/^\s+|\s+$//g for $id,$name; $appid{$id} = $name; } print Dumper \%appid; my $eventxml = 'events.xml'; my $evenxp = XML::XPath->new(filename => $eventxml); my $xpath = "//event/custom_attribute_list/custom_attribute[normalize- +space(name)='SLB_SSRID']/value"; my @eventrecords = (); foreach my $node ($evenxp->findnodes($xpath)) { my $ssrid = $node->string_value; $ssrid =~ s/^\s+|\s+$//g ; if ( exists $appid{$ssrid} ){ push @eventrecords, { eventid => $ssrid }; } } print Dumper \@eventrecords;
---application.xml
<application_list> <application id="2667" external-id="EAR-AA-2667" name="aaaa"></applica +tion> <application id="1103" external-id="EAR-AA-1103" name="bbbb"></applica +tion> <application id="957" external-id="EAR-AA-957" name="cccc"></applicati +on> <application id="1250" external-id="EAR-AA-1250" name="dddd"></applica +tion> </application_list>
-----Events.xml
<event_list> <event> <name> Incident</name> <value> INC0004532345324</value> <name> SSRID</name> <value> 957</value> </event> </event_list>

Replies are listed 'Best First'.
Re: multiple for loop to extract required xml nodes
by haukex (Archbishop) on Jul 11, 2017 at 09:57 UTC

    This appears to be a continuation of your several previous threads, Reaped: Compare to XML files, Compare 2 XML files, and not fetching correct records in xml output, and the code you've posted here is exactly the same as you posted here. As others have said, this is a place to learn Perl, and not a code writing service (usually). Also, duplicate nodes are not appreciated and will often be reaped (deleted).

    Having said that, while it is appreciated that you've posted sample input, code, and expected output, the output does have to match the input, or you have to explain how the output is generated from the input. So for example, why are the eventids 2667 and 1503 duplicated, why are they output in that particular order, etc.

      I am thankful to all for helping me . I make a points you mention .
Re: multiple for loop to extract required xml nodes
by thanos1983 (Parson) on Jul 11, 2017 at 10:53 UTC

    Hello snehit.ar,

    From the sample of code that you provide us and the input data files that you provide us I am not getting the output on the second part of your script.

    It looks like the events.xml file input is not correct. Update.

    Sample of output.

    $ perl test.pl $VAR1 = { '1250' => 'dddd', '957' => 'cccc', '1103' => 'bbbb', '2667' => 'aaaa' }; $VAR1 = [];

    Looking forward to your update, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      try this my $xpath = "//event/custom_attribute_list/custom_attribute[normalize-space(name)='SSRID']/value"; instead of SLB_SSRID

        Hello again snehit.ar,

        Try to replicate the problem solution based on the code that you give us.

        Check the and update accordingly the code before replying! We can not help you with faulty data.

        #!/usr/bin/env perl use strict; use warnings; use XML::XPath; use Data::Dumper; my $xml = 'ApplicationList.xml'; my $xp = XML::XPath->new(filename => $xml); my $appxpath = $xp->findnodes("//application_list/application/"); my %appid = (); foreach my $appnodeset ($appxpath->get_nodelist) { my $id = $xp->find('./@id',$appnodeset)->string_value; my $name = $xp->find('./@name',$appnodeset)->string_value; s/^\s+|\s+$//g for $id,$name; $appid{$id} = $name; } print Dumper \%appid; my $eventxml = 'events.xml'; my $evenxp = XML::XPath->new(filename => $eventxml); my $xpath = "//event/custom_attribute_list/custom_attribute[normalize- +space(name)='SSRID']/value"; my @eventrecords = (); foreach my $node ($evenxp->findnodes($xpath)) { my $ssrid = $node->string_value; $ssrid =~ s/^\s+|\s+$//g ; if ( exists $appid{$ssrid} ){ push @eventrecords, { eventid => $ssrid }; } } print Dumper \@eventrecords; __END__ $ perl test.pl $VAR1 = { '957' => 'cccc', '2667' => 'aaaa', '1250' => 'dddd', '1103' => 'bbbb' }; $VAR1 = [];

        Hope this helps, BR.

        Seeking for Perl wisdom...on the process of learning...not there...yet!