Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^9: Compare 2 XML files

by snehit.ar (Beadle)
on Jul 11, 2017 at 05:53 UTC ( [id://1194775]=note: print w/replies, xml ) Need Help??


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

Hello Poj, i have modify the code as below :
my $xml = 'ApplicationList.xml'; my $xp = XML::XPath->new(filename => $xml); my $appxpath = $xp->findnodes("//application_list/application/"); my %appid = (); my %appname = (); foreach my $appnodeset ($appxpath->get_nodelist) { my $id = $xp->find('./@id',$appnodeset); my $name = $xp->find('./@name',$appnodeset); #$name = $_->string_value; s/^\s+|\s+$//g for $id,$name; $appid{$id} = {$name->string_value}; } print Dumper \%appid;
But i am getting output as
$VAR1 = { '1103' => { 'cccc' => undef }, '2667' => { 'bbbb' => undef }, '957' => { 'aaaa' => undef }, '1503' => { 'dddd' => undef } };
But i want out put as
$VAR1 = { '1103' => 'cccc', '2667' => 'bbbb', '957' => 'aaaa', '1503' => 'dddd' };

Replies are listed 'Best First'.
Re^10: Compare 2 XML files
by huck (Prior) on Jul 11, 2017 at 06:02 UTC

    $appid{$id} = $name->string_value; So what do you think putting braces around then contents did?
    What do braces mean?
    What happens when there isnt an even number of values inside braces?

    You need to understand the answers to those questions or you will just keep getting into trouble.

      Your question really make sense ,but i wont able to complete understand it..Sorry for that.. if i use $appid{$id} = $name->string_value getting this error Odd number of elements in anonymous hash at comparexml.pl line 21. I dont know much in perl ,just got some work todo... How i can resolve it please help ?

        I dont know much in perl ,just got some work todo

        I dont know much in perl ,just got some work i'm trying to get someone to do for me.

        Fixed it for you

Re^10: Compare 2 XML files
by poj (Abbot) on Jul 11, 2017 at 07:08 UTC

    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;
      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}, }; }
        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 }, ];

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-18 02:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found