P is for Practical PerlMonks

### help me with template module to print like this

by veerubiji (Sexton)
 on Nov 09, 2011 at 14:43 UTC Need Help??
veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

I have little problem with printing data like this, I have written script as show below for this input data

$var1={ 'data'=>{ 'employee'=>[ { 'name'=>'suraj', 'number'=>'f11a0478', 'adress'=>' badvel,kadapa,a.p,india', }, { 'name'=>'misra', 'number'=>'047902', 'adress'=>' raipur,ananthapur,a.p,india', }........... ........... ], 'company'=>{ 'name'=>'bosch', 'location'=>'banglore', 'domain'=>{ 'java'=>{ 'employee'=>[ { 'name'=>'suraj', 'number'=>'f11a0478', 'experience'=>{ 'years'=>'3', 'projects'=>'4', } }, { 'name'=>'misra', 'number'=>'047902', 'experience'=>[ { 'years'=>'1', 'projects'=>'1', }, { 'years'=>'1', 'projects'=>'1', } ] }, ] } }................ ................. }; [download] script for above code  #!/usr/bin/perl use warnings; use strict; use Data::Dumper; use XML::Simple; use Template; my$xml = new XML::Simple;
my $data =$xml->XMLin("data.xml", ForceArray=>['dat','employee','expe
+rience']);

#print Dumper($data); my$template = Template->new();
my $filename = 'output1.tex';$template->process(\*DATA, $data,$filename)
|| die "Template process failed: ", $template->error(), "\n"; system( "pdflatex$filename" );
__DATA__
\documentclass[a4paper,leqno,twoside]{article}
\usepackage[latin1]{inputenc}
\usepackage[english]{babel}
\begin{document}
[% FOREACH comp IN company %]
[% comp.name %]
[% comp.location%]
employeedata:
[% FOREACH employee IN comp.domain.java.employee %]

[% employee.name %][% employee.number %]

[% FOREACH obj IN data%]

[% FOREACH beha IN obj.employee %]

[% IF beha.number == employee.number && beha.name == employee.name
+  %]

[% LAST %]
[% END %]
[% END %]
[% END %]
[% END %]
[% END %]
[% END %]
\end{document}
[download]

from above script comparing "name and number" value in two keys in a hash and if both are same print the employee address. Its working and it printing like this as show below.

if there is three persons under employee it printing like this.

if two employee names is there it printing like this.

but I need to print like this
if three employee names is there

if two employee names is there like this

if only one employee name is there like this

[download]

I have maximum number of employee names is three only. I need to print like this help me how to print like this. if any mistakes is there excuse me please.

I tried like this also but problem is

[% FOREACH comp IN company %]
[% comp.name %]
[% comp.location%]
employeedata:
[% FOREACH employee IN comp.domain.java.employee %]

[% employee.name %][% employee.number %]

[% FOREACH obj IN data%]

[% FOREACH beha IN obj.employee %]

[% IF beha.number == employee.number && beha.name == employee.name
+  %]

[%  comp.domain.java.employee.size %]

[%  IF comp.domain.java.employee.size==1 %]

[%  ELSIF comp.domain.java.employee.size==2 %]
[%  ELSIF comp.domain.java.employee.size==3 %]
+].
[% LAST %]
[% END %]
[% END %]
[% END %]
[% END %]
[% END %]
[% END %]
[download]
it is printing like this
if  one employee name under employee tag is there like this

if two employee names is there like this

if three employee names is there like this

[download]

i can't understand what mistake i did and also I found size=3 so i written script like this can any one help me how can i print data as want.

Replies are listed 'Best First'.
Re: help me with template module to print like this
by Eliya (Vicar) on Nov 09, 2011 at 17:32 UTC

So if I'm understanding you correctly, you want to use "or" as a separator before the last entry in the list if the list is longer than 2, and "," otherwise.

The loop iterator supports several methods like .last(), .size(), .count() etc., so you could for example test .last() (which returns true if the current iteration is the last) to determine whether to insert "or" or ","...

exactly you understand my problem.I want print as you said. I tried using .size also but still problems arising. can you see what tried using .size, I posted in main question.Can you tel what mistake i did and how to print data as i want.

[% LAST %] is not the same as loop.last().

The data you've shown is a little too messy for me (to try to fix it up to actually match its usage in the given template...), but the general idea would be something like this:

my $data = { employee => [ { name => 'Foo' }, { name => 'Bar' }, { name => 'Baz' }, ] }; my$template = Template->new();
$template->process(\*DATA,$data)
|| die "Template process failed: ", $template->error(), "\n"; __DATA__ [% FOREACH person IN employee %] [%- IF loop.index() %][% loop.size()>2 && loop.last() ? ' or':',' +%] [% END %] [%- person.name %] [%- END %]. [download] Output: Foo, Bar or Baz. [download] Re: help me with template module to print like this by jethro (Monsignor) on Nov 09, 2011 at 17:47 UTC You don't say which template package you are using. If possible you could create a temporary array with the 1-3 names or first count the names and handle the case with 3 names separately. Or handle the difficult stuff in perl and give the resultant string to the template Re: help me with template module to print like this by Anonymous Monk on Nov 09, 2011 at 17:04 UTC You do not show any of your perl code, so we cannot help you fix what is wrong. Trim your code down to the smallest example which both runs and still exhibits the problem you are having. If you cannot see what is wrong in that trimmed code, someone here probably can. Re: help me with template module to print like this by Anonymous Monk on Nov 10, 2011 at 08:08 UTC Log In?  Username: Password: What's my password? Create A New User Node Status? node history Node Type: perlquestion [id://937092] Approved by herveus help Chatterbox?  [Tanktalus]: choroba: do I have to move? :) Discipulus has too much roots in a 2770 yo town [marioroy]: I want a job after completing MCE in about a week. But feel that I've moved to the wrong place. [choroba]: I fear so. We have offices in Prague, Brno, Saigon and San Francisco. [Discipulus]: choroba if let your company to hire Tanktalus and marioroy ... what a big fishing! [Discipulus]: i choice prague!! [choroba]: 1200 years is enough :-) [marioroy]: Lady_Aleena my$ans = qx! find . -name "*.pl" -exec ls -l {} \\; !; [Tanktalus]: My basic problem right now is that if I were willing to move, I could be back in Toronto with a pay raise. We have a number of reasons for not wanting to do that. [marioroy]: I had to backslash the backslash inside qx.

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2017-04-23 20:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I'm a fool:

Results (432 votes). Check out past polls.