Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Help with recursive function
Message
 
 
À
03/12/2003 19:30:10
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00855676
Message ID:
00855705
Vues:
13
>I need some help creating a little recursive routine. I have a table that i want to have look like this
>department   parent       level
> 1             --           1
> 2              1           2
> 3              1           2
> 4              2           3
> 5              2           3
> 6              3           3
> 7              3           3
>
>The table is created from the below tree.  But only the department and parent are filled in:
>                          1
>
>                        /    \
>                      2        3
>                     / \      / \
>                    4   5    6   7
>
>
>
>I believe I need some sort of recursive routine that will scan the table and fill in the level column based on the above tree.
>Any help greatly, greatly appreciated,
>Thanks,
>Perry

This might be what you are looking for (no recursion required):
Clear
Set Safety Off
Create Table DPL ( iDept I, iParent I, iLevel I )

iInstances = 0
iLevelChangeAt = 1
iNewLevel = 1
For iDepartment = 1 To 17
	iInstances = iInstances + 1
	iNewParent = Int( iDepartment / 2 )
	Insert Into DPL Values ( iDepartment, iNewParent, iNewLevel )
	If iLevelChangeAt	== iInstances Then
		iLevelChangeAt = BitLShift( iLevelChangeAt, 1 )
		iInstances = 0
		iNewLevel = iNewLevel + 1
	Endif
Next iDepartment

Return .T.
And here is a version that determines the level given existing Department (and Parent).
Create Table DPL ( iDept I, iParent I, iLevel I )

For iDepartment = 1 To 66
	Insert Into DPL ;
		Values ( iDepartment ;
			, Int( iDepartment / 2 ) ;
			, 0 )
Next iDepartment

Locate
Browse Last

*- NB iNewLevel has to be private (not Local) so that it is still visible
*- in fGetLevel() an alternative would be to pass it by reference.

iNewLevel = 1
Update DPL Set iLevel = fGetLevel()

Locate
Browse Last NoWait
Return .T.

********************
Function fGetLevel()
********************
	If iDept + 1 > 2 ^ iNewLevel Then
		iNewLevel = iNewLevel + 1
	Endif
	Return iNewLevel
	EndFunc	&& fGetLevel().
censored.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform