Page 1 of 1

Lookup(looping in the ref table)

Posted: Thu Mar 09, 2006 1:12 pm
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

Posted: Thu Mar 09, 2006 1:57 pm
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)

Posted: Thu Mar 09, 2006 2:19 pm
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)

Posted: Thu Mar 09, 2006 5:05 pm
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.

Posted: Fri Mar 10, 2006 12:29 pm
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.

Posted: Fri Mar 10, 2006 1:09 pm
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

Posted: Fri Mar 10, 2006 2:55 pm
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

Posted: Sat Mar 11, 2006 2:51 am
by ArndW
naren - if you have a full pathed hashed file you need to use OPENPATH, not OPEN.

Re: Lookup(looping in the ref table)

Posted: Tue Mar 14, 2006 11:08 am
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

Posted: Wed Mar 15, 2006 3:31 am
by Sunshine2323