Subscribe
Accepted Solution

Function, data types (integer, string), for loop

I have a function that creates a comma-separated list of hostnames which is used as a parameter for and NFS export create command. It works with one exception. It is very short and easy to show it working, and failing.

==== begin-snip ====

def oraExportList( env, clnm, first, last, iface)

{

    exportList = "";

    hostPfx = "xlp";

    for ( int i =  first; i <= last; i++ )

        {

              exportList = exportList + hostPfx + clnm + padNumber(i,2) + "-" + iface + "," ;

        }

    return exportList.substring(0,exportList.length()-1); // trims last character (comma at end-of-line)

}

==== end-snip ====

Hitting the [Test] button and providing the expression ...

         oraExportList("dev","odb",5,"08","sp132")

... works as expected. It iterates from 5 thru 8 and creates a list of formatted hostnames as desired.

          xlpodb05-sp132,xlpodb06-sp132,xlpodb07-sp132,xlpodb08-sp132

HOWEVER, I need the function to accept string parameters. Even thou first and last are used as integer values by the function I need to pass them as strings. Note that the 3rd parameter, named 'first' is passed as: 5 without quotes which is what I would call an integer being passed. The functions starts its simple for-loop by setting i = first and continuing while i <= last.

What I need is for the function to work with this expression ...

         oraExportList("dev","odb","05","08","sp132")

When I call it with first as a string it runs error free but only iterates through the loop 1 time and returns

          xlpodb05-sp132

I've tried to explicitly convert first and last to integers with something like:

     int begin = first;

     int end = last;

    for ( int i =  begin; i <= end; i++ )) { ... }

But no luck. Six-pack of beer to whoever can advise what is going on here. (NOTE: suggesting I write this in PowerShell is not appreciated :-)

Re: Function, data types (integer, string), for loop

You can type-cast MVEL like this:

first = (int) first;

and it will work fine:

==== begin-snip ====

def oraExportList( env, clnm, first, last, iface)

{

    exportList = "";

    hostPfx = "xlp";

   first = (int) first;

    for ( int i =  first; i <= last; i++ )

        {

              exportList = exportList + hostPfx + clnm + padNumber(i,2) + "-" + iface + "," ;

        }

    return exportList.substring(0,exportList.length()-1); // trims last character (comma at end-of-line)

}

====end-snip===


If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.

Re: Function, data types (integer, string), for loop

Awesome! Thank you SInhaa. I knew it had something to do with "type" but I was mistakenly trying things like "first = [int] first" which is more like PowerShell typing syntax.