cancel
Showing results for 
Search instead for 
Did you mean: 

Returning a global variable value in custom function- BODS

former_member200473
Contributor
0 Kudos

Hi Expert,

is there any way to return a global variable which has been initialized inside nested loop(s).

For below given example

$var1 = '0';

$var2 = '1'

$var3 = '';

While ( $var = '1')

begin

$var = $var-1 ;

$var3 = $var1 ;

end

return $var3 ;

i am trying to return $var3 value as output but i get only NULL.

is there any way to return the value.

Accepted Solutions (1)

Accepted Solutions (1)

former_member187605
Active Contributor

Are you calling the fiunction from a dataflow?

The scope of a Global variable set in a function called from a mapping in a Query transform is the dataflow. Yes, you can modify its value based on function logic and use it in the mapping. Once the dataflow is done however, its value is reset to the original one.

former_member200473
Contributor
0 Kudos

Hi Dirk,

yes, i am using function under dataflow and inside query transform.

I keep getting null.

Can you please help me with it ?

Regards,

Shiva Sahu

Answers (2)

Answers (2)

former_member200473
Contributor
0 Kudos

Hi Dirk

I am assured that logic does not have any flaw because testing was done by printing values of global variable;

Below custom function(which you might want to try) was called in script and it printed values as desired (print () is kept inside all loops and if\else)

Logic- to break string in 35 chars or less( if 35 char is space and so on).

$L_inputstring =ltrim( rtrim( replace_substr($P_Input,' ',' '),' '),' '); $G_PLUS = '1'; $G_INC_Counter = '0'; $G_SubString = ''; $L_first_word = ''; $L_second_word = ''; while ((length($G_SubString) < length($L_inputstring))) begin while ( length($L_second_word)< '35' ) begin print( 'inside of ( length($L_second_word)<= 35 )'); $G_INC_Counter = $G_INC_Counter + 2; # Print('$G_INC_Counter'||'-'||$G_INC_Counter); #Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); #$G_INC_Counter = $G_INC_Counter + $G_PLUS ; Print('$G_INC_Counter'||'-'||$G_INC_Counter); $G_SubString1 = $L_First_word; $L_First_word = $L_second_word; $L_second_word = ltrim( ($L_second_word||' '||word_ext($L_inputstring,$G_INC_Counter-1,' ')),' '); Print ( 'length($G_SubString1)'||'-'||length($G_SubString1)||'_'||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); if (length($L_second_word) >= '35') Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); begin $G_INC_Counter = $G_INC_Counter - 1; $G_SubString1 = $L_First_word; end # print( 'inside of ( length($G_SubString)<= 35 )'); # Print('$G_INC_Counter'||'-'||$G_INC_Counter); #$G_INC_Counter = $G_INC_Counter; end #Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); $G_SubString1 = ltrim( rtrim ( $G_SubString1,' '),' '); print ( '$G_SubString1:-'||''||$G_SubString1); print ( '---------------------------------------end of ( length($G_SubString)<= 35 )---------------------------------------'); #------------------------------------------------------------------------------------------ Print ( '-----------------------start of if (length($G_SubString)>= 35 ) and (length($G_SubString) <=70))---------------------------'); $L_first_word = ''; $L_second_word = ''; #while ((length($L_second_word) < '35' ) and (length($G_SubString) < length($L_inputstring))) while (length($L_second_word) < '35' ) begin Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); #print ('else if ((length($G_SubString)>= '35' ) and (length($G_SubString) <=70)))'); # Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); $L_First_word = $L_second_word; $L_second_word = ltrim( ($L_second_word||' '||word_ext($L_inputstring,$G_INC_Counter,' ')),' '); $G_substring2 = $L_second_word; $G_INC_Counter = $G_INC_Counter + '1'; Print ( 'after processing of counter in second loop length($G_SubString2)'||'-'||length($G_SubString2)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); if (length($L_second_word) >= '35') begin $G_INC_Counter = $G_INC_Counter - '1'; $G_SubString2 = $L_First_word ; end end $G_SubString2 = ltrim( rtrim ( $G_SubString2,' '),' '); #Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); Print (' $G_SubString2:-'||' '||$G_SubString2); print ( '------------------------end of else if (length($G_SubString)>= 35 ) and (length($G_SubString) <=70))--------------------------'); # $G_SubString2 = substr( $L_inputstring ,$G_SubString1,(length($G_SubString)-length($G_SubString1))); #------------------------------------------------------------------------------------------ print ( '------------------------------------start of if ((length($G_SubString)>= 70 ) and (length($G_SubString) <=105)--------------------)'); $L_first_word = ''; $L_second_word = ''; #Print ( 'length($G_SubString)'||'-'||length($G_SubString3)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); while (length($L_second_word) < '35' ) begin Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); #print ('else if ((length($G_SubString)>= '35' ) and (length($G_SubString) <=70)))'); # Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); $L_First_word = $L_second_word; $L_second_word = ltrim( ($L_second_word||' '||word_ext($L_inputstring,$G_INC_Counter,' ')),' '); $G_substring3 = $L_second_word; $G_INC_Counter = $G_INC_Counter + '1'; Print ( 'after processing of counter in second loop length($G_SubString3)'||'-'||length($G_SubString3)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); if (length($L_second_word) >= '35') begin $G_INC_Counter = $G_INC_Counter - '1'; $G_SubString3 = $L_First_word ; end end $G_SubString3 = ltrim( rtrim ( $G_SubString3,' '),' '); #Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); Print (' $G_SubString3:-'||' '||$G_SubString3); print ( '----------------end of if ((length($G_SubString)>= 70 ) and (length($G_SubString) <=105))----------------------'); #$G_SubString3 = substr( $L_inputstring ,$G_SubString1,(length($G_SubString)-length($G_SubString1)-length($G_SubString2))); #Print (' $G_SubString3:-'||' '||$G_SubString3); #------------------------------------------------------------------------------------------ print ( '-----------------------------------start of if ((length($G_SubString)>= 105 ) and (length($G_SubString) <=140))----------------------------'); $L_first_word = ''; $L_second_word = ''; Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); while (length($L_second_word) < '35' ) begin Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); #print ('else if ((length($G_SubString)>= '35' ) and (length($G_SubString) <=70)))'); # Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); $L_First_word = $L_second_word; $L_second_word = ltrim( ($L_second_word||' '||word_ext($L_inputstring,$G_INC_Counter,' ')),' '); $G_substring4 = $L_second_word; $G_INC_Counter = $G_INC_Counter + '1'; Print ( 'after processing of counter in second loop length($G_SubString2)'||'-'||length($G_SubString4)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); if (length($L_second_word) >= '35') begin $G_INC_Counter = $G_INC_Counter - '1'; $G_SubString4 = $L_First_word ; end end $G_SubString4 = ltrim( rtrim ( $G_SubString4,' '),' '); #------------------------------------------------------------------------------------------ print ( '-----------------------------------start of if ((length($G_SubString)>= 140 ) and (length($G_SubString) <=175))----------------------------'); $L_first_word = ''; $L_second_word = ''; Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); while (length($L_second_word) < '35' ) begin Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); #print ('else if ((length($G_SubString)>= '35' ) and (length($G_SubString) <=70)))'); # Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); $L_First_word = $L_second_word; $L_second_word = ltrim( ($L_second_word||' '||word_ext($L_inputstring,$G_INC_Counter,' ')),' '); $G_substring5 = $L_second_word; $G_INC_Counter = $G_INC_Counter + '1'; Print ( 'after processing of counter in second loop length($G_SubString2)'||'-'||length($G_SubString4)||'_'||'$G_INC_Counter'||'-'||$G_INC_Counter||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); if (length($L_second_word) >= '35') begin $G_INC_Counter = $G_INC_Counter - '1'; $G_SubString5 = $L_First_word ; end end $G_SubString5 = ltrim( rtrim ( $G_SubString5,' '),' '); #Print ( 'length($G_SubString)'||'-'||length($G_SubString)||'_'||'$L_first_word'||'-'||$L_first_word||'-'||'$L_second_word'||'-'||$L_second_word); print ('-----------------------------------end of if ((length($G_SubString)>=140 ) and (length($G_SubString) <=105))--------------------------'); #print ( '$G_SubString1||'_'||$G_SubString2||'_'||$G_SubString3||'_'||$G_SubString4||'_'||--'||'-'$G_SubString1||'_'||$G_SubString2||'_'||$G_SubString3||'_'||$G_SubString4); $G_SubString = $G_SubString1||'_'||$G_SubString2||'_'||$G_SubString3||'_'||$G_SubString4||'_'||$G_SubString5; Print (' $G_SubString and its lenght -'||' '||$G_SubString ||'-'||length($G_SubString)); Print (' $G_SubString1 and its lenght:-'||' '||$G_SubString1||'-'||length($G_SubString1)); Print (' $G_SubString2 and its lenght:-'||' '||$G_SubString2||'-'||length($G_SubString2)); Print (' $G_SubString3 and its lenght:-'||' '||$G_SubString3||'-'||length($G_SubString3)); Print (' $G_SubString4 and its lenght:-'||' '||$G_SubString4||'-'||length($G_SubString4)); Print (' $G_SubString5 and its lenght:-'||' '||$G_SubString5||'-'||length($G_SubString5)); print ( 'lenghth of $G_SubString is '||'-'||length($G_SubString)); print ( 'lenghth of $L_inputstring is '||'-'|| length($L_inputstring)); $P_Input = $G_SubString1||'_'||$G_SubString2||'_'||$G_SubString3||'_'||$G_SubString4||'_'||$G_SubString5; print(' ----------------------------------------------------end of parent loop'); end $P_Input = $G_SubString1||'_'||$G_SubString2||'_'||$G_SubString3||'_'||$G_SubString4||'_'||$G_SubString5; return '<$P_Input>';

Please let me know if you could see any flaw in my logic or if there was any suggestion.

former_member187605
Active Contributor
0 Kudos

That may be due to a flaw in your logic. What's the value of $var? Maybe your loop is never executed, and therefore null returned.