Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

problem with hash iteration in template

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

Hi, I have XML file with more number of university's like this as shown below

<university> <name>svu</name> <location>ravru</location> <branch> <electronics> <student name="xxx" number="12"> <semester number="1"subjects="7" rank="2"/> </student> <student name="xxx" number="15"> <semester number="1" subjects="7" rank="10"/> <semester number="2" subjects="4" rank="1"/> </student> <student name="xxx" number="16"> <semester number="1"subjects="7" rank="2"/> <semester number="2"subjects="4" rank="2"/> </student> </electronics> </branch> </university> <university> <name>sku</name> <location>ANTP</location> <branch> <electronics> <student name="xxx" number="12"> <semester number="3"subjects="6" rank="20"/> </student> <student name="xxx" number="16"> <semester number="1"subjects="9" rank="12"/> <semester number="2"subjects="4" rank="2"/> </student> </section> </electronics> </branch> </university>

I used XML::Simple to store this data into one hash data structure variable $data and that information is like this

$var1={ university=>{ 'name'=>'svu', 'location'=>'ravru', 'branch'=>{ 'electronics'=>{ 'student'=>[ { 'name'=>'xxx', 'number'=>'12', 'semester'=>{ 'Number'=>'1', 'subjects'=>'7', 'rank'=>'1' } }, { 'name'=>'xxx', 'number'=>'15', 'semester'=>[ { 'number'=>'1', 'subjects'=>'7', 'rank'=>'10' }, { 'number'=>'1', 'subjects'=>'7', 'rank'=>'1' } ] }, { 'name'=>'xxx', 'number'=>'16', 'semester'=>[ { 'number'=>'1', 'subjects'=>'7', 'rank'=>'2' }, { 'number'=>'2', 'subjects'=>'4', 'rank'=>'2' } ] } } ] } }; university=>{ 'name'=>'sku', 'location'=>'ANTP', 'branch'=>{ 'electronics'=>{ 'student'=>[ { 'name'=>'xxx', 'number'=>'12', 'semester'=>{ 'Number'=>'3', 'subjects'=>'6 +', 'rank'=>'20' } }, { 'name'=>'xxx', 'number'=>'6', 'semester'=>[ { 'number'=>'1', 'subjects'=>'9', 'rank'=>'12' }, { 'number'=>'2', 'subjects'=>'4', 'rank'=>'2' } ] } } ] } };

by using template tool kit and pdflatex compiler I am generating PDF file , I written like this as shown below.

my $template = Template->new(); my $filename = 'output.tex'; $template->process(\*DATA, $data, $filename) || die "Template process failed: ", $template->error(), "\n"; system( "pdflatex $filename" ); __DATA__ \documentclass[a4paper,leqno,twoside]{article} \begin{document} [% FOREACH st IN university %] [% %] [%st.location%] studentdata: [% FOREACH student IN st.branch.electronics.student %] Component type: [%+ +%][%+ student.number +%] [% FOREACH semester IN student.semester %] [%+ semester.number +%] [%+ semester.subjects +%] [%+ semester.rank +%] [% END %] [% END %] [% END %]

But I getting Different output rather than what I want. After executing this script I am getting output when I have three or more students in electronics and two or more semesters for each student then only I am getting. otherwise it does print any thing just printing studentdata: three times . for example if I have one student with one semester in university at that situation it doesn't give any thing it printing like this studentdata: studentdata: studentdata: like as above ,I identified the problem is I have three attributes in student element "name""number" and "semester" like this so it printing three times If I add one more attribute in between name and number in student element it printing four times. It means something wrong with second for loop but I cant rectify it. I tried alot . how can I rectify and how can I print data like this in pdf.

name: svu location: ravru student data: student name=xxx number=12 semester number=1 subjects=7 rank=2 student name=xxx number=15 semester number=1 subjects=7 rank=10 semester number=2 subjects=4 rank=1 student name=xxx number=16 semester number=1 subjects=7 rank=2 semester number=2 subjects=4 rank=2

I want to print like this can any body help me.

Replies are listed 'Best First'.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2016-06-27 03:42 GMT
Find Nodes?
    Voting Booth?
    My preferred method of making French fries (chips) is in a ...

    Results (335 votes). Check out past polls.