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

Get string values before regex

#!/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"); # no + end / 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;

Replies are listed 'Best First'.
Re^11: Compare 2 XML files
by snehit.ar (Beadle) on Jul 11, 2017 at 07:20 UTC
    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 .
      if ( exists $appid{$ssrid} ){ push @eventrecords, { eventid => $ssrid, name => $appid{$ssrid}, }; }
        its working , thanks you so much !!!
      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;
        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 },