From: Peter S. <sh...@ya...> - 2005-04-14 21:52:44
|
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. __________________________________ Do you Yahoo!? Yahoo! Small Business - Try our new resources site! http://smallbusiness.yahoo.com/resources/ |
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. > > > > > |
From: Marc P. <ma...@an...> - 2005-04-15 08:57:28
|
Keats Kirsch wrote: > 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. RFE for WM (why don't we get a free open source JIRA install?)... which I may be able to turn myself too but it's probably wise to get a go-ahead first ;-) In the introspection code, when failures occur, I believe we should make the error messages like this simpler to understand for non-Java programmers. i.e. we could quite easily detect that class name Lxxxxx is an array, so we should say so in the message. i.e. "No such method contains() found on object, which is an array of java.lang.Object" I'm not sure there are any other changes along those lines, I remember a lot of work went into this with failed property introspections to there's probably not that much that can be improved. Cheers -- Marc Palmer wj...@wa... Wangjammers - Java, J2ME and Web Consultants ~ http://www.wangjammers.org/ |