I often deal with structured arrays (LoL), say 

[%a = [0, [1,2],[3,4], [4,5,6]] %]

and I need to print them in some form, sometimes just flattened. 

Join doesn't do the job, and neither merge (it is undocumented, isn't 
it?) 

I wrote a generalization of join (I need a better name than jj) 


sub jj {
  my $list = shift;
        my $joint = defined $_[0] ? shift @_ : ' ';
        my @temp = ();
        foreach (@$list) {
                if (ref($_) eq "ARRAY") {
                        push @temp, jj($_, @_ ? @_ : $joint);
                } else {
                        push @temp,  defined $_ ? $_ : '';
                }
        }                               
        return join $joint, @temp;
};

that behaves like join but works on LoL. 

[%a = [0, [1,2],[3,4], [4,5,6]] %]

a.jj: [% a.jj %]
a.jj(','):  [% a.jj(',') %]
a.jj(',',';'): [% a.jj(',',';') %]

gives

a.jj: 0 1 2 3 4 4 5 6
a.jj(','):  0,1,2,3,4,4,5,6
a.jj(',',';'): 0,1;2,3;4,4;5;6

you can notice that the last separator (' ' by default) is repeated if 
needed. 

For more complex tasks I wrote an extension (still needing a better name)

sub mm {
  my $list = shift;
        my $format = defined $_[0] ? shift @_ : ['%s', ' ', '%s'];
        $format = ['%s', $format, '%s'] unless ref $format eq 'ARRAY';
        my @temp = ();
        foreach (@$list) {
                if (ref($_) eq "ARRAY") {
                        push @temp, mm($_, @_ ? @_ : $format);
                } else {
                        push @temp,  defined $_ ? $_ : ' ';
                }
        }                               
        $format->[0] = '%s' unless defined $format->[0];
        $format->[1] = ' ' unless defined $format->[1];
        $format->[2] = '%s' unless defined $format->[2];
        map {$_=sprintf  $format->[2], $_} @temp;
        my $s=join $format->[1], @temp ;
        return sprintf $format->[0], $s;
};

that behaves like jj, but can also be called with a vector

var.mm([listformat, separator, elementformat]) 

such that each element is first printed (sprintf) using elementformat
('%s' by default), then joined using separator (' ' by default)  and
finally printed using listformat ('%s' by default). if the argument is a 
scalar it is taken as seperator. 


[%a = [0, [1,2],[3,4], [4,5,6]] %]
  
a.mm: [% a.mm %]
a.mm(','):  [% a.mm(',') %]
a.mm(',',';'): [% a.mm(',',';') %]
a.mm(['(%s)', ',']): [% a.mm(['[%s]', ',']) %]
a.mm(["<ol>\n%s\n</ol>", "\n", "<li>%s</li>"]):
 [% a.mm(["<ol>\n%s\n</ol>", "\n", "<li>%s</li>"]) %]


gives 

a.mm: 0 1 2 3 4 4 5 6
a.mm(','):  0,1,2,3,4,4,5,6
a.mm(',',';'): 0,1;2,3;4,4;5;6
a.mm(['(%s)', ',']): (0,(1,2),(3,4),(4,5,6))
a.mm(["<ol>\n%s\n</ol>", "\n", "<li>%s</li>"]):
 <ol>
<li>0</li>
<li><ol>
<li>1</li>
<li>2</li>
</ol></li>
<li><ol>
<li>3</li>
<li>4</li>
</ol></li>
<li><ol>
<li>4</li>
<li>5</li>
<li>6</li>
</ol></li>
</ol>

hoping it can be useful. Is there a contrib repository? 


-- 
Franco Bagnoli (franchino) <[EMAIL PROTECTED]> ([EMAIL PROTECTED])
virtual location: Dipartimento di Energetica "S. Stecco"
real location: Dip. Matematica Applicata "G. Sansone", Universita' Firenze,
Via S. Marta, 3 I-50139 Firenze, Italy. Tel. +39 0554796422, fax: +39 055471787
GPG Key fingerprint = 169D 9EA5 8FD3 7EDA E43A  9830 255F BCEC 0D63 3728



_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to