Lookup(looping in the ref table)

Post questions here relative to DataStage Server Edition for such areas as Server job design, DS Basic, Routines, Job Sequences, etc.

Moderators: chulett, rschirm, roy

Post Reply
naren6876
Participant
Posts: 233
Joined: Mon Feb 07, 2005 7:19 pm

Lookup(looping in the ref table)

Post by naren6876 »

Hi,

I have a input record like:


col1 col2 col3
66 41 55

Ref table data like:

col1 col2 col3 Status
66 41 55 1 66 41 56
66 41 56 1 66 41 57
66 41 57 0 66 41 58

What i need to do is, I have to look for the record with 66 41 55 in the ref data and If finds the record then check for the status. If the status = 1 then transfers the vaalues 66 41 56.

And again look for the record with this new values in the ref data if finds check the status and tranfers the values if status =1. I need to do this until status=0 finally capture those values of 66 41 58.

How can i achieve this?.(Looping in the ref data).

I would be grateful for any assistance.

Thanks,
Naren
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

Naren,
this can be done with either a recursive lookup or a loop within a routine. You could chain lookups in a job, but you could only link back one level per lookup - if you know your maximum depth of recursion you could do it in a job directly.
You could solve it with a relatively short DS routine. How that is coded depends upon how you defined your key (single column or several ones)
naren6876
Participant
Posts: 233
Joined: Mon Feb 07, 2005 7:19 pm

Post by naren6876 »

could anyone please shed some light on how to write the recursive routine.

Thanks in advance.
ArndW wrote:Naren,
this can be done with either a recursive lookup or a loop within a routine. You could chain lookups in a job, but you could only link back one level per lookup - if you know your maximum depth of recursion you could do it in a job directly.
You could solve it with a relatively short DS routine. How that is coded depends upon how you defined your key (single column or several ones)
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

Provide a DEFFUN declaration that refers to the function itself.

Code: Select all

DEFFUN MyRoutine(Arg1, Arg2) Calling DSU.MyRoutine
Then, within your routine, you can invoke it recursively. Make sure to give yourself an exit strategy.
Example

Code: Select all

FUNCTION Factorial(X)
DEFFUN Factorial(Arg1) Calling "DSU.Factorial"
If X > 1 And X < 20
Then
   Ans = X * Factorial((X - 1))
End
Else 
   Ans = 1  ; * argument out of range
End
RETURN(Ans)
This really also needs a Matches "1N0N" test to guarantee that the argument is a positive integer.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
naren6876
Participant
Posts: 233
Joined: Mon Feb 07, 2005 7:19 pm

Post by naren6876 »

Hi all,

Let me put my requirement in this way,

Input Data:

a,b
b,c
c,d

Output Data should be like

a,d
b,d
c,d

Any clues pls.

Thanks
Naren
ray.wurlod wrote:Provide a DEFFUN declaration that refers to the function itself.

Code: Select all

DEFFUN MyRoutine(Arg1, Arg2) Calling DSU.MyRoutine
Then, within your routine, you can invoke it recursively. Make sure to give yourself an exit strategy.
Example

Code: Select all

FUNCTION Factorial(X)
DEFFUN Factorial(Arg1) Calling "DSU.Factorial"
If X > 1 And X < 20
Then
   Ans = X * Factorial((X - 1))
End
Else 
   Ans = 1  ; * argument out of range
End
RETURN(Ans)
This really also needs a Matches "1N0N" test to guarantee that the argument is a positive integer.
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

Naren,

Here is a shell program that is a "hint" - it doesn't use COMMON to skip re-opening the file and only uses a simple key, one column with your 0 or 1 STATUS and one column with the next key. Don't just copy this and run it - there is no error checking or handling and you should go through each line to understand what is happening so that you can finish the routine to your specifications.

Code: Select all

Routine GetFinalStatus(Key)
DEFFUN GetFinalStatus(Key) CALLING 'DSU.GetFinalStatus'
COMMON/GetFinalStatusCommon/FilePointer
IF NOT(FilePointer) THEN OPEN '','HashReferenceFile' TO FilePointer ELSE CALL DSLogFatal('oops','')
READ XrefRecord FROM FilePointer,Key ELSE XrefRecord = ''
IF XrefRecord<1>='0' ;** 1st field of the hashed file
THEN
   ** We have a valid exit
   Ans = XrefRecord<2> ; set return value
END
ELSE
   Ans = GetFinalStatus(XrefRecord<2>)
END
naren6876
Participant
Posts: 233
Joined: Mon Feb 07, 2005 7:19 pm

Post by naren6876 »

Hi,

Here, iam not able to open my Hash file by giving the full path.It always going to else part only.
could you please explain it.
ArndW wrote:Naren,

Here is a shell program that is a "hint" - it doesn't use COMMON to skip re-opening the file and only uses a simple key, one column with your 0 or 1 STATUS and one column with the next key. Don't just copy this and run it - there is no error checking or handling and you should go through each line to understand what is happening so that you can finish the routine to your specifications.

Code: Select all

Routine GetFinalStatus(Key)
DEFFUN GetFinalStatus(Key) CALLING 'DSU.GetFinalStatus'
COMMON/GetFinalStatusCommon/FilePointer
IF NOT(FilePointer) THEN OPEN '','HashReferenceFile' TO FilePointer ELSE CALL DSLogFatal('oops','')
READ XrefRecord FROM FilePointer,Key ELSE XrefRecord = ''
IF XrefRecord<1>='0' ;** 1st field of the hashed file
THEN
   ** We have a valid exit
   Ans = XrefRecord<2> ; set return value
END
ELSE
   Ans = GetFinalStatus(XrefRecord<2>)
END
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

naren - if you have a full pathed hashed file you need to use OPENPATH, not OPEN.
shahid
Participant
Posts: 1
Joined: Mon Sep 20, 2004 9:42 am

Re: Lookup(looping in the ref table)

Post by shahid »

Isn't it just simpler to use the hash lookup utility.
naren6876 wrote:Hi,

I have a input record like:


col1 col2 col3
66 41 55

Ref table data like:

col1 col2 col3 Status
66 41 55 1 66 41 56
66 41 56 1 66 41 57
66 41 57 0 66 41 58

What i need to do is, I have to look for the record with 66 41 55 in the ref data and If finds the record then check for the status. If the status = 1 then transfers the vaalues 66 41 56.

And again look for the record with this new values in the ref data if finds check the status and tranfers the values if status =1. I need to do this until status=0 finally capture those values of 66 41 58.

How can i achieve this?.(Looping in the ref data).

I would be grateful for any assistance.

Thanks,
Naren
Sunshine2323
Charter Member
Charter Member
Posts: 130
Joined: Mon Sep 06, 2004 3:05 am
Location: Dubai,UAE

Post by Sunshine2323 »

Warm Regards,
Amruta Bandekar

<b>If A equals success, then the formula is: A = X + Y + Z, X is work. Y is play. Z is keep your mouth shut. </b>
--Albert Einstein
Post Reply