cancel
Showing results for 
Search instead for 
Did you mean: 

UDF for reading a particular line and storing the value in a variable

Former Member
0 Kudos

Hi Experts,

I am reading entire file content in single feild in source data type.I want to write a UDF to seach for a particular pattern(say :25: and store the value after it i.e 34535764778 detination for further use).

EX: My sample file and its has feilds like

}

point assured

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

HI Sri,

You are sending entire data to a single field as array. right?

If yes then create a advanced udf

//write your code here

String s="";

for(int i=0;i<a.length;i++){

int start = a.indexOf(u201C:25:u201D);

int end = a.indexOf(u201C:28Cu201D);

s = a.substring(start+4,end);

}

+result.addValue(s);+

The above udf gives the output as 34535764778

If any changes required let me know.

Regards

Sridhar Goli

Former Member
0 Kudos

Hi i tried the UDF given by you, it shows errors when i run the mapping test

47: illegal character: 8220 int start = a.indexOf(âu20ACu0153:25:âu20AC?); ^

47: ')' expected int start = a.indexOf(âu20ACu0153:25:âu20AC?); ^

i have taken input as the data which i have givenin my thread.

Please let me know if you can figure out the reason for this error.

Former Member
0 Kudos

Hi

As you are passing all the date to single string field

Try simple UDF(value as cache)


String output="";
		for(int i=0;i<input.length();i++){
		    int start = input.indexOf(":25:");
		    int end = input.indexOf(":28C");
		    output = input.substring(start+4,end);
		}
return output;

Thanks

Gaurav

Answers (3)

Answers (3)

Former Member
0 Kudos

The code worked fine thanks

Former Member
0 Kudos

Hi since u are using a single field only then u dont need to use any advanced function

Wel because if u see the formation of the queue u wil find the the whole data is under one context.. and there are not more context changes

Convert the string to an array and progressively u can check for the four checks if the a<i>=:

then a[i+]=2 then a[i]=5 and a[i+] =: and then substring the next 7-8 chrac.. hope u understand

If this also doesnt work wel then u will have to use the Pattern class in Java

By the way remember that the substring value cant be retained for the next instance of mapping

Hope u have that in mind...

Rgds

Aditya

Shabarish_Nair
Active Contributor
0 Kudos

the logic should be as follows;

create an advanced UDF.

1. Loop for the length of the array (queue) of the input.

2. search for the index of :25:

3. if index is >= 0, then extract the substring of the last index of : to the length of the current string

thats it