in reply to Re^10: Compare 2 XML files
in thread Compare 2 XML files

You are really a monk saver for me ...!!! Please do help me with 1more small query ... as per the code u shared yesterday i am getting eventid and name in diff arrays.. But Now i want out put as :

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;
Thanks for helping .

Replies are listed 'Best First'.
Re^12: Compare 2 XML files
by poj (Abbot) on Jul 11, 2017 at 08:06 UTC
    if ( exists $appid{$ssrid} ){ push @eventrecords, { eventid => $ssrid, name => $appid{$ssrid}, }; }
      its working , thanks you so much !!!
Re^12: Compare 2 XML files
by snehit.ar (Beadle) on Jul 12, 2017 at 13:04 UTC
    I'm working on below code and want to sort the records with event id and then add index number to each array .. But know i am first getting whole list of event ids and then line ..
    #!/usr/bin/env perl use strict; use warnings; use XML::XPath; use Data::Dumper; 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 ; push @eventrecords, { eventid => $ssrid }; } @eventrecords = sort{ $b->{eventid} <=> $a->{eventid} } @eventrecords; + #Sorting my $index; my @recordcount; for (@eventrecords) { $index++; push @recordcount, { line => $index }; } splice @eventrecords, $index, 0, @recordcount; print Dumper \@eventrecords;
    $VAR1 = [ { 'eventid' => 2667 }, { 'eventid' => 2666 }, { 'eventid' => 2656 }, { 'line' => 1 }, { 'line' => 2 }, { 'line' => 3 ];

    expected code

    $VAR1 = [ { 'eventid' => 2667 'line' => 1 }, { 'eventid' => 2666 'line' => 2 }, { 'eventid' => 2656 'line' => 3 }, ];

      In that case you will need to alter the inner hashrefs, not the outer array, viz:

      #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my @eventrecords = ( { eventid => 2667 }, { eventid => 2666 }, { eventid => 2656 } ); for my $i (0 .. $#eventrecords) { $eventrecords[$i]->{line} = $i + 1; } print Dumper \@eventrecords;
        Thanks @Hippo for helping .
      hello, I have an array to store event ids ,But on my dashboard rows are restricted to 6. So if my array returns 10 eventid, i want to set line 7-10 as blank . ..Similarly if event id have only 3 records set remaining 4-10 as blank

      expected output

      { 'line' => 1, 'eventid' => 2667 }, { 'line' => 2, 'eventid' => 2667 }, { 'eventid' => 2666, 'line' => 3 }, { 'eventid' => 2666, 'line' => 4 }, { 'line' => 5, 'eventid' => 2666 }, { 'line' => 6, 'eventid' => 2666 }, { 'eventid' => "", 'line' => 7 }, { 'line' => 8, 'eventid' => "" }, { 'eventid' => "", 'line' => 9 }, { 'eventid' => "", 'line' => 10 },
        Try
        #!perl use strict; use Data::Dump 'pp'; # test data my @AoH = map{ { line => $_, eventid => int rand(2000) } } 1..8; my $max = 6; for (@AoH){ if ($_->{'line'} > $max){ $_->{'eventid'} = ''; } } for (@AoH+1..$max){ push @AoH, { line => $_, eventid => '' }; } pp \@AoH;
        poj