My English is little bit poor so don't hesitate me if didn't understand. I have problem with perl template tool kit module. This is my first experience using this module.I am reading xml data from file and I used XML::Simple(ForceArray) to parse and DATA::Dumper to print. Printing data is
$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',
}
]
},
]
}
}................
.................
};
I used template toolkit process like this and printed in some file, I written code like this
[% FOREACH comp IN company %]
[% comp.name %]
[% comp.location%]
employeedata:
[% FOREACH employee IN comp.domain.java.employee %]
employee name: [% employee.name +%] numb [%+ employee.number %]
[% FOREACH experience IN employee. experience %]
[% experience.years %]
[% experience.projects %]
[% END %]
[% END %]
[% END %]
From above script I am able to print company employee, name, number, experience, years ,projects. But my problem is I need to add address of employee also to this information. I am printing all information in company node and for all company's. But Address is located some other key in the hash. By using employee name and number(some employee having same name some having same number) I need to find the address and print the address in the employee details. help me with this problem. What should I do now, how to solve this problem.
You could do:
[% FOREACH de IN data.employee %]
[% IF de.name == employee.name && de.number == employee.number %]
[% de.address %]
[% LAST %]
[% END %]
[% END %]
I'd recommend doing something about the data structure before processing it with TT though, because this isn't very efficient. If comp.domain.java.employee and data.employee contains the same number of elements it's pretty straight forward.
EDIT: What I meant in my comment below is
[% FOREACH employee IN comp.domain.java.employee %]
[% addresses = [] %]
[% FOREACH de IN data.employee %]
<!-- Name, experience etc. -->
<!-- Then new address stuff: -->
[% IF de.name == employee.name || de.number == employee.number %]
[% addresses.push(de.address) %]
[% END %]
[% END %]
[% addressStr = addresses.join(', '); addressStr.replace(', ([^,]+)$', ' or $1') %]
[% addressStr %]
[% END %]
That's not printing addresses like address1, address2 or address3
?