[stringtemplate-interest] How to strip() elements from an attribute by a condition, not just null values

Terence Parr parrt at cs.usfca.edu
Thu Dec 11 10:21:34 PST 2008


On Dec 11, 2008, at 4:54 AM, Igor Stolbov wrote:
>             var template = "$numbers:{$if(it.Value)$$it.Key$$endif 
> $}; separator=\",\"$";
>             var onlyEvenNumbers = new StringTemplate(template);
>             onlyEvenNumbers.SetAttribute("numbers", numbers);
>             Console.Out.WriteLine(onlyEvenNumbers.ToString());
>         }
>     }
> }
>
>
> Naïve approach would be to use
>
>                                 {$if(it.Value)$$it.Key$$endif$}
>
> template, so that only odd keys would get propagated. Good enough,  
> but there would still remain all extra commas that separate even  
> keys, even though the keys themselves get stripped by the template.   
> So, the output of the program above would be
>
> 1,,3,,5
>
> while I need to get
>
> 1,3,5

I'm surprised that doesn't work!  Hmm...try $else$$endif$ also to see  
if that makes the value go away.

Also try $strip(numbers:{$if(it.Value)$$it.Key$$endif$})$ to see if  
that works.

> Another approach would be to transform the initial multi-value  
> attribute to another multi-value attribute so that all even values  
> would become null in that new attribute, and then use strip() to  
> filter out all those nulls from it. The problem is that I do not  
> know how to express such transformation in terms of String Templates.
>

I hope the above does that.

Ter



More information about the stringtemplate-interest mailing list