Conditional string replacement

Hello!

I’m importing a csv file that has a column Month with the value “2015/3”.

What I want to do is convert it to 201503, however a simple replace of “/” with “0” won’t do since Oct, Nov and Dec have 2 digits and the output will be 2015012 and I want to keep the same length. Desired for Dec will be 201512.

My first approach was to use reformat component and add a new field in the metadata output called Processed_month set as string.

Under transformation I’m trying to use some sort of conditional clause like below:

if(length($in.0.Month) == 6) replace($in.0.Month, "/", "0"); else replace($in.0.Month, "/", "");

…there is some sort of syntax error and the statement is not valid.

Would love to have some feedback as I’m just starting with Clover 3.1 and trying to automate some processes.

Thanks in advance for your replies!
Aronax

Hi Aronax,

The clause you are trying to carry out is correct, what is missing is the output port that is mandatory if you want to forward the result to the next component :


function integer transform() {

if(length($in.0.Month) == 6) {

 $out.0.Month=replace($in.0.Month, "/", "0"); 

 }
else 

 $out.0.Month=replace($in.0.Month, "/", "");

 return ALL;
}

Please note that the Clover you are currently using is obsolete and is no longer supported, I suggest to upgrade to the newest version 4.0.4 .

Hi & thanks for the reply,

So can I add this function code in the transformation component?

found a workaround…maybe someone else can benefit… :

iif(isnull($in.0.Month),"null",iif(length($in.0.Month) == 6,replace($in.0.Month, "/", "0"),replace($in.0.Month, "/", "")))

it’s not by choice I use this version… it is the latest version we can use at the moment…

Thanks again,
Aronax

Yes, you can use both codes in the transformation component as you have probably already tested. Just reminding that you have to specify the output port and metadata field.



$out.0.Month = iif(isnull($in.0.Month),"null",iif(length($in.0.Month) == 6,replace($in.0.Month, "/", "0"),replace($in.0.Month, "/", "")))