arithmetic

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

arithmetic

dousti
Hello,

I have a simple macro which is supposed to receive a netMask (say
255.255.0.0) and convert it to an inverseMask 0.0.255.255.

The first problem is: how can I split on "."? I tried ".", "\.", "\\."
and so forth but none worked.

The second question is: given a string "128", how can I subtract it
from 255? I tried:

#set( $byte = "128" )
#set( $result = 255 - Integer.parseInt($byte) )

But, velocity doesn't like Integer.parseInt() static method.

Thanks in advance,

--
Ramin

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: arithmetic

Jason Pettiss
You should really try to avoid being too clever in a template.  Java is
good for doing logical operations.  Templates are good for creating
content.  Try to keep the business logic in Java.

Btw, to split on '.' in Java you'd need to do "\\.".  You need to escape
the backslash so that a literal backslash goes to the regexp evaluator.  
But in a template you'd just need something like $mask.split('\.').  
Note the use of single quotes!

If you want to use a static method you need to wrap it with an instance
method.  As long as you're doing that why not just implement the full
method to do what you need.  Make life easier for yourself.  Write
yourself a method which does the right thing.  And toss an instance of
that object into your Context.

Jason Pettiss
[hidden email]

Ramin Dousti wrote:

>Hello,
>
>I have a simple macro which is supposed to receive a netMask (say
>255.255.0.0) and convert it to an inverseMask 0.0.255.255.
>
>The first problem is: how can I split on "."? I tried ".", "\.", "\\."
>and so forth but none worked.
>
>The second question is: given a string "128", how can I subtract it
>from 255? I tried:
>
>#set( $byte = "128" )
>#set( $result = 255 - Integer.parseInt($byte) )
>
>But, velocity doesn't like Integer.parseInt() static method.
>
>Thanks in advance,
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: arithmetic

dousti
Thanks, Jason. I have access only to templates for doing what I want
to do. Meaning, I have no access to the java code behind it. So, I guess
I'm out of luck there to do what you're suggesting.

Having said that, is there any way I can get "255 - $value" with $value
being a string?

Ramin

On 5/25/05, Jason Pettiss <[hidden email]> wrote:

> You should really try to avoid being too clever in a template.  Java is
> good for doing logical operations.  Templates are good for creating
> content.  Try to keep the business logic in Java.
>
> Btw, to split on '.' in Java you'd need to do "\\.".  You need to escape
> the backslash so that a literal backslash goes to the regexp evaluator.
> But in a template you'd just need something like $mask.split('\.').
> Note the use of single quotes!
>
> If you want to use a static method you need to wrap it with an instance
> method.  As long as you're doing that why not just implement the full
> method to do what you need.  Make life easier for yourself.  Write
> yourself a method which does the right thing.  And toss an instance of
> that object into your Context.
>
> Jason Pettiss
> [hidden email]
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: arithmetic

Jason Pettiss
I'm mainly posting this as a joke.  So keep that in mind.  But.  You
could do something like, um... well...

---
#set( $mask = '0.1.254.255' )
#set( $imask = '' )

#macro( numchar $char $x $m )
#if( $char=='0' )#set( $num = 0 )
#elseif( $char=='1' )#set( $num = 1 )
#elseif( $char=='2' )#set( $num = 2 )
#elseif( $char=='3' )#set( $num = 3 )
#elseif( $char=='4' )#set( $num = 4 )
#elseif( $char=='5' )#set( $num = 5 )
#elseif( $char=='6' )#set( $num = 6 )
#elseif( $char=='7' )#set( $num = 7 )
#elseif( $char=='8' )#set( $num = 8 )
#else( $char=='9' )#set( $num = 9 )
#end
#set( $x = $x + $num * $m )
#end
#set( $bytes = $mask.split('\.') )
#foreach( $byte in $bytes )
#if( 3 == $byte.length() )
    #set( $h = $byte.substring(0,1) )
    #set( $t = $byte.substring(1,2) )
    #set( $o = $byte.substring(2,3) )
#elseif( 2 == $byte.length() )
    #set( $h = '0' )
    #set( $t = $byte.substring(0,1) )
    #set( $o = $byte.substring(1,2) )
#else
    #set( $h = '0' )
    #set( $t = '0' )
    #set( $o = $byte.substring(0,1) )
#end
#set( $x = 0 )
#numchar( $o $x 1 )
#numchar( $t $x 10 )
#numchar( $h $x 100 )
#set( $x = 255 - $x )
#set( $imask = "${imask}.$x" )
#end
#set( $imask = $imask.substring(1) )

Your mask was <b>$mask</b><br/>
Your inverse mask is <b>$imask</b>
---

That will print out

Your mask was *0.1.254.255*
Your inverse mask is *255.254.1.0*

Hehe.

Jason Pettiss
[hidden email]

Ramin Dousti wrote:

>Thanks, Jason. I have access only to templates for doing what I want
>to do. Meaning, I have no access to the java code behind it. So, I guess
>I'm out of luck there to do what you're suggesting.
>
>Having said that, is there any way I can get "255 - $value" with $value
>being a string?
>
>Ramin
>
>On 5/25/05, Jason Pettiss <[hidden email]> wrote:
>  
>
>>You should really try to avoid being too clever in a template.  Java is
>>good for doing logical operations.  Templates are good for creating
>>content.  Try to keep the business logic in Java.
>>
>>Btw, to split on '.' in Java you'd need to do "\\.".  You need to escape
>>the backslash so that a literal backslash goes to the regexp evaluator.
>>But in a template you'd just need something like $mask.split('\.').
>>Note the use of single quotes!
>>
>>If you want to use a static method you need to wrap it with an instance
>>method.  As long as you're doing that why not just implement the full
>>method to do what you need.  Make life easier for yourself.  Write
>>yourself a method which does the right thing.  And toss an instance of
>>that object into your Context.
>>
>>Jason Pettiss
>>[hidden email]
>>
>>    
>>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: [hidden email]
>For additional commands, e-mail: [hidden email]
>
>  
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]