Hi, I have the xml file file this, perl script to create hash

<university> <name>svu</name> <location>ravru</location> <branch> <electronics> <student name="mikky" number="12"> <semester number="1"subjects="7" rank="2"/> </student> <student name="vijju" number="15"> <semester number="1" subjects="7" rank="10"/> <semester number="2" subjects="4" rank="1"/> </student> <student name="shyam" number="16"> <semester number="1"subjects="7" rank="2"/> <semester number="2"subjects="4" rank="2"/> </student> </electronics> </branch> </university>. . . . . . <data> <student name="mikky" number="1" branch="computers" /> <student name="vijju" number="2" branch="electronics" /> <student name="shyam" number="3" branch="EEE" /> <student name="csr" number="4" branch="IT" /> <student name="msr" number="5" branch="MEC" /> <student name="ssr" number="6" branch="computers" /> <student name="msr" number="1" branch="CIV" /> ............................. .............................. ..................... </data>

how to create hash table for data elements. name and number as the key and branch is the value in that hash. why I need like this means some students have same name and some students have same number. by using this hash keys I have to search in the university node for student if found and print branch name of each student.

I written some script in XML::Simple but I am not able to create hash.

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use XML::Simple; my $xml = new XML::Simple; my $data = $xml->XMLin("data.xml", forcearray => [ 'student' , 'semist +er'], + KeyAttr=>{student=>"+Name"}); print Dumper($data);
its printing like this by using data dumper.
$var1={ 'data'=>{ 'student'=>[ { 'name'=>'mikky', 'number'=>'12', 'branch'=>'computers', }, { 'name'=>'vijju', 'number'=>'15', 'branch'=>'electronics', }, { 'name'=>'shyam', 'number'=>'16', 'branch'=>'EEE', }, ], 'university'=>{ 'name'=>'svu', 'location'=>'ravru', 'branch'=>{ 'electronics'=>{ 'student'=>[ { 'name'=>'mikky', 'number'=>'12', 'semester'=>{ 'Number'=>'1', 'subjects'=>'7', 'rank'=>'2' } }, { 'name'=>'vijju', 'number'=>'15', 'semester'=>[ { 'number'=>'1', 'subjects'=>'7', 'rank'=>'10' }, { 'number'=>'2', 'subjects'=>'4', 'rank'=>'1' } ] }, { 'name'=>'shyam, 'number'=>'15', 'semester'=>[ { 'number'=>'1', 'subjects'=>'7', 'rank'=>'2' }, { 'number'=>'2', 'subjects'=>'4', 'rank'=>'2' } ] }, ] } } };

but I need to print only data node elements only not university node elements.please halp me.


In reply to help me in perl script to create hash and print some data. by veerubiji

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.