Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

help me in perl script to create hash and print some data.

by veerubiji (Sexton)
on Nov 03, 2011 at 17:46 UTC ( #935726=perlquestion: print w/replies, xml ) Need Help??
veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: perl script to create hash
by Anonymous Monk on Nov 03, 2011 at 19:02 UTC
Re: help me in perl script to create hash and print some data.
by Jenda (Abbot) on Nov 04, 2011 at 09:58 UTC

    Have a look at XML::Rules. Examples may be found right here on PerlMonks.

    Enoch was right!
    Enjoy the last years of Rome.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://935726]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2017-02-20 01:06 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (293 votes). Check out past polls.