Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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.

Comment on help me in perl script to create hash and print some data.
Select or Download Code
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.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2014-09-20 20:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (161 votes), past polls