On Tue, 23 Sep 2003, Clayton L. Scott wrote:
> --- template ---
>
> Grep is not behaving as I would expect particularly in the case of a
> pattern that looks like '^number$'
Here are my findings for the archives:
grep was working as advertised. Here's a small self-contained test:
[% SET values = [ 1 .. 21 ] %]
[% SET match = 1 %]
[% SET pat = "^$match\$" %]
[% pat %]:
[% values.grep( pat ).join("\n") %]
My real problem was that I was assuming that an empty list
would evaluate to false in a boolean context.
> Is there some escaping or interpolation that I'm missing with pat in
> this usage? (don't mind the whitespace differences)
>
> TIA,
> Clayton
>
> [% SET field = {
> options => { '1' => 'Chickens',
> '12' => 'Ducks' ,
> '22' => 'Geese' },
> ,
> values => [ '1' ],
> }
> %]
>
> Expected output like:
> '22' => 'Geese'
> selected '1' => 'Chickens'
> '12' => 'Ducks'
>
> Actual:
> [%#
> selected '22' => 'Geese'
> selected '1' => 'Chickens'
> selected '12' => 'Ducks'
> %]
> [% FOREACH optn = field.options.keys %]
> [% SET v = field.options.$optn %]
> [% SET pat = '^' _ optn _ '$' %]
> [% field.values.grep( pat ) ? "selected" : ''%] '[% optn %]' =>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The underlined statement was the is the problem, I was assuming that if
the returned list was empty it would be false in this context, which it
is not.
I changed this to:
[% field.values.grep( pat ).size == 1 ? "selected" : ''%]
And it worked pefectly.
Clayton
_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates