Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

help me with template module to print like this

by veerubiji (Sexton)
on Nov 09, 2011 at 14:43 UTC ( #937092=perlquestion: print w/ replies, xml ) 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', } ] }, ] } }................ ................. };
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 + %] [% beha.address %], [% LAST %] [% END %] [% END %] [% END %] [% END %] [% END %] [% END %] \end{document}

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. person address1, person2 address, person3 address, if two employee names is there it printing like this. person address1, person2 address,, but I need to print like this if three employee names is there person address1, person2 address or person3 address. if two employee names is there like this person address1, person2 address. if only one employee name is there like this person address1.

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 %] [% beha.address %]. [% ELSIF comp.domain.java.employee.size==2 %] [% beha.address %],[% beha.address %]. [% ELSIF comp.domain.java.employee.size==3 %] [% beha.address %],[% beha.address %]or[% beha.address % +]. [% LAST %] [% END %] [% END %] [% END %] [% END %] [% END %] [% END %]
it is printing like this
if one employee name under employee tag is there like this person address1. if two employee names is there like this person address1, person1 address. person address2, person2 address. if three employee names is there like this person address1, person1 address or person1 address. person address2, person2 address or person2 address. person address3, person3 address or person3 address.

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.

above hole post I written person1 address instead of his address for easy writing purpose.

Comment on help me with template module to print like this
Select or Download Code
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 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 ","...

    See also Loop Processing.

      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 %].

        Output:

        Foo, Bar or Baz.
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 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?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-09-20 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (159 votes), past polls