Search code examples
perltemplate-toolkit

error in printing data using template module


I have little problem with printing data like this, I have written script like this

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

[% FOREACH experiance IN employee.experiance %]

[% FOREACH obj IN ObjectDefinition%]

[% FOREACH beha IN obj.experiance %]

[% IF beha.years == experiance.years %] 

 [% beha.Name %],
  [% LAST %]
 [% END %]
   [% END %]
 [% END %]
 [% END %]
[% END %]
  [% END %]

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

if three names is there it printing like this.

  clar, larson, per,

if two names is there it printing like this.

clar, larson,

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

clar, larson or per.

if two names is there like this

clar, larson.

if only one name is there like this

clar.

I have maximum number of 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

 [% FOREACH employee IN comp.domain.java.employee %]

[% FOREACH experience IN employee.experience %]

[% FOREACH obj IN ObjectDefinition%]

[% FOREACH beha IN obj.experience %]

 [% IF beha.years == experience.years %]

 [% IF employee.experience.size == 1 %]
 [% beha.Name %].


 [% ELSIF employee.experience.size == 2 %]
     [% beha.Name %],[% beha.Name %].


[% ELSIF employee.experience.size == 3 %] 
 [% beha.Name %],[% beha.Name %]or[% beha.Name %].
 [% END %]
  [% END %]
  [% END %]
  [% END %]
  [% END %]
  [% END %]

but it printing when we have one name. if we have two names it printing like this

clar,clar.
larson,larson.

if we have three names it printing like this

clar,clar or clar.
larson,larson or larson.
per,per or per.

what is wrong with my script I cant solve this problem can any body help me please.


Solution

  • The second attempt is definitely closer to what you're going to need. But the order of the loops doesn't look right. beha.Name is only ever going to have the value of the current record.

    I think you want something like this:

    [%  SET staff = []; # empty list 
        FOREACH beha IN obj.experience;
            staff.push(beha) IF beha.years == experience.years;
        END; %]
    [%  IF staff.size == 1;
            staff.0.Name;
        ELSIF staff.size == 2;
            "$staff.0.Name, $staff.1.Name";
        ELSIF staff.size >= 3;
            "$staff.0.Name, $staff.1.Name or $staff.2.Name";
        END %] 
    

    The IF-ELSIF loop is not very pretty, but there's no obvious staff.join() construction that's going to do what you need with that mix of commas and text separators and a limit of 3 elements.

    UPDATE

    As requested, an example using .join:

    If you had an arbitrary length list of names, you might want to present them as name, name, [name, ...] or name:

    ELSIF staff.size >= 3;
        SET lastname = staff.pop; #remove last person from list
        staff.join(', '); " or $lastname";
    END;
    

    But as I said previously, with a maximum of 3 names, this is overkill.