Newbie 'Replace' Question

I’m a total newbie only starting to use the tool yesterday!

I have a simple CSV input file where I want to replace the values in one column with a different value. The field currently contains values such as “Y”, “N”, “NA”, blanks etc. I want to replace these with “XXX”, “YYY”, “ZZZ” etc.

I have used the Reformat Transformer to apply replace($in.0.Part,“Y”,“XXX”) but I just get an error. The field is a string.

The source code looks like this $out.0.Part = replace($in.0.Part,“Y”,“XXX”);

I have applied the same syntax to another field which is also a string and it works… replacing “-” with “/” for example.

I can’t see what I’m doing wrong… any advice you can give would be greatly appreciated as I’m already pulling my hair out!

Thanks

Dave

Hi Dave,

Could you please share your graph and a sample of input file throwing the error? I would like to see the error you get for myself.

A general advice I can give you right away is that you should be careful about the second argument of replace function. It is not a string as it might seem, it is a regular expression. So for example special characters should be escaped. See the documentation for replace function http://doc.cloveretl.com/documentation/ … l2-replace

Thanks.

Hi Lubos,

Thanks for your help.

Attached you can see the graph and the transformation I have tried to apply that failed.

Then I have shown the graph processing successfully without that Reformat box and an excerpt of the data that shows the field I’m trying to apply the transformation on.

The strange thing is I used the same syntax to apply a similar transformation on another field (replacing - with / in a string) and it worked!

Dave

Ultimately I will want to replace several different values with new ones (Y, N, NA, blank etc). Would I need to have some kind of if, then, else statement to achieve this?

Sorry if this is too basic I’m a glorified user not a developer! :slight_smile:

Thanks, Dave

Hi Dave,

I think that Lubos wanted to take a look at the GRF file (or even better the whole project including the source CSV file). To me, it doesn’t seem there is actually a problem with the replace function. The thing is the issue might have been caused by several other reasons. In order to find this out I would need to see the long and/or the GRF file.

Regarding the if - else (or switch - case) statements, it really depends on the particular case. Sometimes even one smart regular expression can handle multiple (or all) different cases.