Need help to implement recursion in WTX

Formally known as "Mercator Inside Integrator 6.7", DataStage TX enables high-volume, complex transactions without the need for additional coding.

Moderators: chulett, rschirm

Post Reply
Amar_nath
Participant
Posts: 56
Joined: Tue Apr 18, 2006 5:03 am

Need help to implement recursion in WTX

Post by Amar_nath »

Hello,

I am new to WTX, need help in implementing a simple recursive logic. Input 1 is a plain text and input 2 is an integer number which is fixed length. Requirement is to get an output file with lines of fixed length given in input2.

e.g. Input 1 = abcdefgh Input2= 3
Output-
abc
def
gh

I am thinking of using LEFT function recursively.. but not sure how to store the intermediate values and use it again. i.e. if I do LEFT (Input1, Input2) it gives "abc" but after tht Input1 should be "defgh".. I am not able to achieve this.

Thanks
jorgecox
Participant
Posts: 1
Joined: Tue Aug 31, 2010 9:38 am

Re: Need help to implement recursion in WTX

Post by jorgecox »

I would use the CLONE function for this.

This is quite advanced stuff, but I will try and describe how I would do this...

Your output card would need a type tree that has multiple data items in, i.e.

Group
Item(s)

In the Map, you need to call a functional map at the Item(s) level with the CLONE command:

e.g. =F_create_recs ( CLONE ( "DUMMY" , 1 + (SIZE ( Input1 ) / Input2 ) ) , Input1, Input2, INDEX($) )

Your functional map will then have 4 input cards - some wont be automatically resolved and you should in the functional map wizard set it to a suitable type tree and item (must already be used in the map somewhere else).

In1 = DUMMY
In2 = Input1's data
In3 = Input2's data
In4 = Sequence No

In the functional map set the output as follows:

=MID (In2,1+(In3 * (In4-1)), In3)

I have assumed Input1 is a text based item and Input2 is a number based item else you may need to use TEXTTONUMBER in the CLONE/MID rules.

You may need to play with the CLONE command to ensure you get the last bit of data from the end of the file that doesnt match the record size supplied in Input2 - I have added 1 to the 'SIZE' calculation but this will be a problem for an exact number. You could use the MOD function on the SIZE of Input1 and if an exact number then dont add 1 else add 1 or use the ROUNDUP function in some way.

Basically for your example the CLONE command will run the functional map based on Input1 size and the value of Input2 - for your example it would be 1 + ( 8 / 3 ) = 3 times

The INDEX($) will pass an incrementing number for each time the function map runs i.e. 1 then 2 then 3.

DUMMY is ignored in the functional map - and can be any text you like

The MID function will then essentially do the following:
1st run of functional map
=MID (In2,1,3)
2nd run of functional map
=MID (In2,4,3)
3rd run of functional map
=MID (In2,7,3)

Hope this helps.
jgibby
Participant
Posts: 42
Joined: Thu Dec 16, 2004 8:48 am

Post by jgibby »

I haven't been on here for a while, and I know this thread isn't that new. However, I think this can be accomplish without recursion or functional maps, no offense intended at all to JorgeCox. It looks as if his solution will work, I just wanted to offer a different one.

Given that Input1 is a string "abcdefgh" and Input2 is number 3, create an output card that uses a text object in a series. In the formula for the series text field, use the following formula:

Code: Select all

=CLONE("*" ,1 + INT( SIZE(Input1) / Input2 ) )
Now create a second output card of the same type as the first, series text object and in the series text field formula enter this:

Code: Select all

=MID( Input1 ,1+( Input2 * ( INDEX(TextField:Output1) - 1) ) ,Input2 )
It worked successfully on my end so let me know if it works for you.

John Gibby
"Artificial intelligience is no match for natural stupidity."
Post Reply