Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

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.name %] [%st.location%] studentdata: [% FOREACH student IN st.branch.electronics.student %] Component type: [%+ student.name +%][%+ student.number +%] [% FOREACH semester IN student.semester %] [%+ semester.number +%] [%+ semester.subjects +%] [%+ semester.rank +%] [% END %] [% END %] [% END %]
\end{document}

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.

Comment on problem with hash iteration in template
Select or Download Code
Reaped: Re: problem with hash iteration in template
by NodeReaper (Curate) on Nov 01, 2011 at 12:34 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (12)
As of 2014-09-23 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (219 votes), past polls