velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "bluejoe" <bluejoe2...@gmail.com>
Subject Re: simple problem
Date Tue, 21 Jul 2009 00:25:16 GMT
I did it in the same way in my projects, and I defined class MathHelper just using Velocity:)

public Object calculate(String expr)
{
    String vtl = "#set($_ooo=" + expr + ")";
    VelocityContext vc = ...
    Velocity.evaluate...
    return vc.get("_ooo");
}

---bluejoe

----- Original Message ----- 
From: "Alexander" <the.malkolm@gmail.com>
To: "Velocity Users List" <user@velocity.apache.org>
Sent: Monday, July 20, 2009 9:59 PM
Subject: Re: simple problem


> 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 <lanxiazhi@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 <lanxiazhi@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.gupta1@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:lanxiazhi@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.malkolm@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
>> >>
>> >>
>> >>
>> >>
>> >
>>
>
Mime
View raw message