ok,you're right,now i got what you mean. just a moment ago,I realized that I need no such math helper or such compicated macro as I wrote at all.If I have a velocity tool class called Simple,which contains a method : double f(double d) {return d}; then I can use it in the template:$Simple.f($a+100*$b).This should work,I think.[?] 2009/7/20 Alexander <the.malk...@gmail.com>
> I meant passing such string without single quote to method as parameter > eventually leads to same result: variables replaced with values and > expression calculated. But using some math helper class is more flexible > than writing your own macros. > > 2009/7/20 lanxiazhi <lanxia...@gmail.com> > > > hello,try it this way,with single quote, > > #expr('$a+$b*300') > > which works as expected. > > > > 2009/7/20 Alexander <the.malk...@gmail.com> > > > > > lanxiazhi, > > > > > > > > > > by the way,besides evaluating math expression ,my macro can insert > > > > varibles in it. > > > > > > like expr("$a+100*$b"). > > > > > > > > > Before you passing "$a + 100*$b" to method parameters Velocity > > > automatically > > > gonna replace all variables with values, remember? > > > > > > > #macro(calc $expr) > > > > > > $mathHelper.calculate($expr) > > > > > > #end > > > > > > > > > $mathHelper.calculate("$a + 100*$b") > > > > > > > > > 2009/7/20 lanxiazhi <lanxia...@gmail.com> > > > > > > > Yes,Alexander,and I google it,and i found one choice:jep.and maybe > > javacc > > > > is > > > > another choice for this. > > > > by the way,besides evaluating math expression ,my macro can insert > > > > varibles > > > > in it. > > > > like expr("$a+100*$b"). > > > > still I think this is not a good way to do it,maybe next version of > > > > velocity > > > > will support this expression eval internally,which I think is not a > > hard > > > > task for the talented developers. > > > > the macro: > > > > #macro(expr $expression) > > > > #set($temp--='#set($temp---'+"=$expression)"+'$temp---') > > > > #evaluate($temp--) > > > > #end > > > > 2009/7/20 Alexander <the.malk...@gmail.com> > > > > > > > > > Maybe you should find some class that can parse and evaluate math > > > string > > > > > (like "1+2+3*3 + 8/2") andthen use it in macro? > > > > > > > > > > #macro(calc $expr) > > > > > $mathHelper.calculate($expr) > > > > > #end > > > > > > > > > > I see you are trying to write ur own parser and I dont think It is > a > > > good > > > > > idea. There should be a lot of > > > > > such classes in internet, as writing calculator is ordinary simple > > task > > > > for > > > > > beginner, huh? > > > > > > > > > > 2009/7/20 lanxiazhi <lanxia...@gmail.com> > > > > > > > > > > > Hello,everyone,I work on the problem this whole afternoon... > > > > > > finally,I decided to end this with a macro: > > > > > > #macro(expr $expression) > > > > > > #set($temp--='#set($temp---'+"=$expression)"+'$temp---') > > > > > > #evaluate($temp--) > > > > > > $temp--- > > > > > > #end > > > > > > #set($a=2) > > > > > > #expr("$a*100+200") > > > > > > > > > > > > which result in a 400. > > > > > > wait for an easier way from you . > > > > > > thanks > > > > > > > > > > > > 2009/7/20 lanxiazhi <lanxia...@gmail.com> > > > > > > > > > > > > > thanks Rupali, > > > > > > > I got this problem when I started to think that, can I simply > > > output > > > > an > > > > > > > expression like #{1+2+200000*3+$a} without a varible? > > > > > > > > > > > > > > 2009/7/20 Rupali Gupta <r.gup...@mpstechnologies.com> > > > > > > > > > > > > > > Hi, > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> Use $velocityCount in place where you need incremental value > of > > > some > > > > > > >> variable > > > > > > >> > > > > > > >> Eg: instead of, > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> #set($a = 0) > > > > > > >> > > > > > > >> #foreach($mapping in $mappings) > > > > > > >> > > > > > > >> Do something.. > > > > > > >> > > > > > > >> #set($a = $a+1) > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> <label for="journal-$a">Type something</label> > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> Do it like: > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> #foreach($mapping in $mappings) > > > > > > >> > > > > > > >> Do something.. > > > > > > >> > > > > > > >> <label for="journal- $velocityCount ">Type something </label> > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> $velocityCount increase the value of variable incrementally, > > > without > > > > > > >> having > > > > > > >> to use ($a= $a+1) > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> I hope this helps! > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> Thanks, > > > > > > >> > > > > > > >> Rupali > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> _____ > > > > > > >> > > > > > > >> From: lanxiazhi [mailto:lanxia...@gmail.com] > > > > > > >> Sent: Monday, July 20, 2009 12:52 PM > > > > > > >> To: Velocity Users List > > > > > > >> Subject: Re: simple problem > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> we know this #set($a = $a + 1) will change $a which in some > > cases > > > > may > > > > > > not > > > > > > >> be > > > > > > >> preferable...but thanks anyway. > > > > > > >> > > > > > > >> 2009/7/20 Alexander <the.malk...@gmail.com> > > > > > > >> > > > > > > >> Hi, > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> Use > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> #set($a = $a + 1) > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> Correct me if Im wrong but if u use variable like this it > would > > > > write > > > > > > >> "hello" on a single line, where is the problem? > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> #set($newline="hello") > > > > > > >> > > > > > > >> $newline > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > >> > > > > > > > > > > > > > > > > > > > > > > > > > > > >