From: Keats K. <ke...@xa...> - 2005-04-14 22:14:55
|
The $List tool can solve your problem. The "Ljava.lang.Object" is the JVMs polite way of telling you that $myList is an array of Objects, which is what WMScript uses to implement the square-bracket list syntax. This has always been a pain point in WMScript, but it is not easy to fix without breaking a lot of legacy templates. The $List tool gives you a pretty convenient workaround. You can see its methods in the JavaDocs: http://www.webmacro.org/api/org/webmacro/servlet/ListUtil.html. You can say: #if ($List.contains($myList,$compositeObj.name)) { ... or #if ($List.toList($myList).contains($compositeObj.name)) { ... or, using the function version: #if ($toList($myList).contains( ... or #set $myList = $toList($myList) #if ($myList.contains(... etc. Hope this does it for you. - Keats Peter Smith wrote: >I want to be able to find out if $myVar is in $mylist. > Is there syntax to handle this w/ WM template >scripting? > > ## Here is my list > #set $myList = [ "one", "two", "three" ] > > ## Here is the variable I'm looking for > #set $myVar = "two" > > ## I want to do this > #if ( $myVar in $myList ) { > do something... > } > #else { > do something else... > } > >In my particular case, $myList is a java.util.Vector >made up of composite objects - each object has a name, >value, etc. To keep maintenance simple, I wanted to >avoid doing a multiple-condition 'if' statement like >this: > > #if ( ($myVar==$possibility1) || > ($myVar==$possibility2) || > ($myVar==$possibility3) > etc. ) ) { > do something... > } > #else... > > >To give a fuller picture, my close-to-ideal >pseudo-code looks like this: > > #set $myList = [ "one", "two", "three" ] > > #foreach $compositeObj in $someRuntimeList { > > #if ($compositeObj.name in $myList) { > do something > } > #else { > do something else... > } > > } > >I've tried to turn around the 'if' condition using >this syntax: > > #if ($myList.contains($compositeObj.name)) { > >but I get a 'NoSuchMethodException on a >Ljava.lang.Object'. > >Thanks for any pointers. > >I'm willing to do some coding if we wanted to add this >to the codebase - only, not sure if I could even >understand enough of the engine (Expression.java >maybe?) to know where to start hacking. > > > > > |