Help me with perl script.

Hi,
I am beginner to perl programming language. I have large XML file it contains information about all diagnostic tests and their descriptions. each diagnostic test having one code like "1100" and regarding description in xml comments. for example my xml code like this

      <data>
             <testnumber>1100</testnumber>
               <cause>battery short circuit</cause>
                <description> check proper connection</description>
      </data>

like as i shown in above i have more number of tests in my xml file. now i have to create hash table using perl script. in the hash table testnumber as a key and description as a value. finally i need to create hash table with all testnumbers and according descriptions.

start by looking at the available XML modules. The CPAN Search Site - search.cpan.org

Hi,
i started reading that modules but i cant able how to create hash table by reading xml files.

ok - so you have a working script to parse the XML document correctly and now you want to build a hash of diagnostic codes? Please post the code you have done already.

Hi frank,
I am posting my code what i tried. i am reading a xml file and extracting information <DTC> to </DTC>. because my test number and test description starts and ends with that comments. i am posting my original code also here.

<DTC>
     </DTC>

my perl script:

#!/usr/bin/perl
use warnings;
use strict;
 
 open(File1, "formal.xml") or die "una
+ble to open file: $!\n";
 open(File2, ">formal.xml") or die "unable to open file: $!\n";
 while(<File1>)
{
chomp;
print "$_\n";
print File2 "$_\n";
}
close (File1);
close(File2);
 
while (<File>)
{
   if (m|^\s*<DTC>| ... m|^\s*</DTC>|)
   {
    print;   
   }
}

but now i dnt know how to create hash table with this data.I have 100 testcodes and their descriptions like as same as above.

Hi,
My code is working to extract the content but i dnt know how to build hash table using perl script.

Please try the XML::Simple module. It is much simplier than write a parser by yourself.

XML::Simple - search.cpan.org

Hi,
After your suggetion i tried by using xml::simple, i have written like this

#!/usr/bin/perl
use warnings;
use strict;
use Data::Dumper; 


  number :1100 description: { <data>.......
</data>}.
  number code :1200 description: { <data>.......
.............
</data>}.....

but i am able to printing
trouble code : 1100
i am not able to print description like above by using my code . what should i have to do in this case.
thanks in advance.

You output still need to keep the original xml content between "<dtc>" and "</dtc>" ?

$
$
$ cat data.xml
<foo>
  <data>
    <testnumber>1100</testnumber>
    <cause>battery short circuit</cause>
    <description>check proper connection</description>
  </data>
  <data>
    <testnumber>1200</testnumber>
    <cause>Some other cause</cause>
    <description>Some other description</description>
  </data>
</foo>
$
$
$ cat -n parsedata.pl
     1  #!perl -w
     2  use XML::Parser;
     3  my $tag;
     4  my $key;
     5  my %testnums;
     6  my $str;
     7
     8  sub handle_start {
     9    my ($parseinst, $element, %attrs) = @_;
    10    $tag = $element;
    11  }
    12  sub handle_end {
    13    my ($parseinst, $element, %attrs) = @_;
    14    $tag = "";
    15  }
    16  sub handle_char {
    17    my ($parseinst, $data) = @_;
    18    if (defined $tag and $tag eq "testnumber") {
    19      $key = $data;
    20    } elsif (defined $tag and $tag eq "description") {
    21      $testnums{$key} = $data;
    22    }
    23  }
    24
    25  {
    26    local $/=undef;
    27    open (FH, "<", "data.xml") or die "Can't open file: $!";
    28    $str = <FH>;
    29    close (FH) or die "Can't close file: $!";
    30  }
    31  $p1 = XML::Parser->new(Handlers => {Start => \&handle_start,
    32                                      End   => \&handle_end,
    33                                      Char  => \&handle_char});
    34  $p1->parse($str);
    35
    36  # print the hash
    37  while (my ($k, $v) = each %testnums) {
    38    print "Key = $k, Value = $v\n";
    39  }
$
$ perl parsedata.pl
Key = 1200, Value = Some other description
Key = 1100, Value = check proper connection
$
$

tyler_durden

yes, i need to keep original xml data as same as it is . but my output must be testnumber and according description below like that.

---------- Post updated at 10:32 PM ---------- Previous update was at 10:29 PM ----------

yes

---------- Post updated 10-04-11 at 09:07 AM ---------- Previous update was 10-03-11 at 10:32 PM ----------

reply,
Thank you very much for your reply and i tried this one it prints the description line only but under description i need to print the xml original data as it is. like
testcode :1100
description: <data>
<testnumber>1100</testnumber>
<cause>battery short circuit</cause>
<description> check proper connection</description>
</data>
testcode :1200
description: <data>
<testnumber>1200</testnumber>
<cause>battery short circuit</cause>
<description> check proper connection</description>
</data>.............
like this. thanks in advance.

#!/usr/bin/perl

use warnings;
use strict;
use XML::Simple;

my %data = %{XMLin('yourdata.xml')};
print "testcode: ".$data{'Testnumber'}."\n";
print "description: ".XMLout(\%data, RootName => 'data', NoAttr => 1)."\n";

exit(0);

This assumes the xml file contains only one element. The output looks like this:

*** Deleted ***

Post your sample input and expected output within code tags.
I am unable to understand your expected output because the text of your post runs into your output.

Click the hyperlink below to read the guidelines for adding code tags.

The UNIX and Linux Forums - Forum Rules

tyler_durden

---------- Post updated at 07:15 PM ---------- Previous update was at 07:13 PM ----------

[quote=macmonster;302561528]

#!/usr/bin/perl

use warnings;
use strict;
use XML::Simple;

This assumes the xml file contains only one element. The output looks like this:

[code]

Hi, thank you very much for helping and my outpuy same like this but more number of tests. i need to print all tests and descriptions like same as above. i will try this one.

---------- Post updated at 08:00 PM ---------- Previous update was at 07:55 PM ----------

[quote=macmonster;302561528]

#!/usr/bin/perl

use warnings;
use strict;
use XML::Simple;

my %data = %{XMLin('yourdata.xml')};
print "testcode: ".$data{'Testnumber'}."\n";
print "description: ".XMLout(\%data, RootName => 'data', NoAttr => 1)."\n";

exit(0);

This assumes the xml file contains only one element. The output looks like this:

Just deleted. :slight_smile:

Thank you ver much.