Page 1 of 1 1
Topic Options
#42491 - 2003-07-07 02:43 PM Functions
David Nicholls Offline
Fresh Scripter

Registered: 2002-01-02
Posts: 24
Loc: London, UK
How do I return multiple values from a funtion?

Can I pass and return an array from a function?

In the script I am writing at the moment, I am trying to break it up into a series of function calls. I would like to be able to return an array from a function alongside a number of other variables

Hope you can help [Smile]

Top
#42492 - 2003-07-07 02:48 PM Re: Functions
Shawn Administrator Offline
Administrator
*****

Registered: 1999-08-13
Posts: 8611
Yup, sure can, here's an example function that returns an array of two numbers ... the numbers that were passed as arguments ...



Break On
 
$Array = AsArray(1,2)
 
?"First=" $Array[0]
?"Second=" $Array[1]
 
Exit 1
 
Function AsArray($Num1,$Num2)
 
 $AsArray = $Num1$Num2
 
EndFunction



-Shawn

[ 07. July 2003, 14:50: Message edited by: Shawn ]

Top
#42493 - 2003-07-07 02:51 PM Re: Functions
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Please read the FAQ Forum under How to write a UDF .

Basically, you can return only one variable from a UDF. However, you can return an array of values as a variable. The UDF Library contains lots of examples that return arrays.

Example
code:
$a=filesplit('c:\winnt\system32\kixforms.dll')
for each $b in $a
? 'Folder/file = '+$b
next
function filesplit($file)
$filesplit=split($file,'\')
endfunction

_________________________
There are two types of vessels, submarines and targets.

Top
#42494 - 2003-07-07 04:56 PM Re: Functions
David Nicholls Offline
Fresh Scripter

Registered: 2002-01-02
Posts: 24
Loc: London, UK
I thought I had it conceptually understood, but this little segment doesnt work... I have obviously misunderstood somehting somewhere [Frown]

(The needs to be replaced by something valid on your network naturally [Smile]

code:
$acctDomain = "<put your account domain here>"

$groupMembership = udfInDomainGroup($AcctDomain)

$domainsize=UBOUND($groupMembership)

? ? "********* Counting Back From Returned Array*********"
? "Total Number of Groups: " + $domainsize
FOR each $member in $groupMembership
? "Member of :" $member
NEXT


FUNCTION udfInDomainGroup($AcctDomain)

;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
;+Obtain User Domain Group Membership in an array +
;+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

? "Get Global Group memberships..."
;$text = "Enumerating Group Membership"
;addLogText($Text)

Dim $udfInDomainGroup[1]
$Index = 0
$domGroupCount = 0
? "Account Domain:" + $AcctDomain
DO
$Group = EnumGroup($Index)
$splitGroup = Split($Group,"\")
IF UBOUND ($splitGroup) = 1
IF $splitGroup[0] = $AcctDomain
;? $splitGroup[1]
$udfInDomainGroup[$domGroupCount] = $splitGroup[1]
$domGroupCount = $domGroupCount + 1
;? $udfInDomainGroup[($domGroupCount - 1)]
REDIM PRESERVE $udfInDomainGroup[$domGroupCount]
;? $udfInDomainGroup[$domGroupCount - 1]
ENDIF
ENDIF
$Index = $Index + 1
UNTIL LEN($Group) = 0
$domGroupCount = $domGroupCount - 1

? ? "********* Counting Back From Array*********"
? "Total Number of Groups: " + $domGroupCount ?

For each $member in $udfInDomainGroup
? " Member of :" + $member
next

ENDFUNCTION


Top
#42495 - 2003-07-07 05:08 PM Re: Functions
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
You might want to take a look at the UDF Forum, e.g. UserGroups() - returns all groups of a given user or GetGroups() - Another group UDF with a slight twist
_________________________
There are two types of vessels, submarines and targets.

Top
#42496 - 2003-07-07 05:11 PM Re: Functions
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
I think this is what you are after:
code:
Function udfInDomainGroup($sDomain)
Dim $sGroup
Dim $iIndex

$iIndex=0
$sGroup=EnumGroup($iIndex)
While $sGroup
If Instr($sGroup,$sDomain+"\")=1
Redim Preserve $udfInDomainGroup[Ubound($udfInDomainGroup)+1]
$udfInDomainGroup[Ubound($udfInDomainGroup)]=SubStr($sGroup,InStr($sGroup,"\")+1)
EndIf
$iIndex=$iIndex+1
$sGroup=EnumGroup($iIndex)
Loop
Return
EndFunction


Top
#42497 - 2003-07-07 05:37 PM Re: Functions
David Nicholls Offline
Fresh Scripter

Registered: 2002-01-02
Posts: 24
Loc: London, UK
Jens, Richard thanks both again for your help

Retrieving the group memberships works fine and populates the array, however its returning the array out of the function I am getting wrong [Frown]

Please teach me how to fish [Big Grin] I've quite a few other segments like this I have to work on in my script

Top
#42498 - 2003-07-07 05:41 PM Re: Functions
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
I normally use a temporary array in my UDFs and do the return value assignment at the very end of the function.
_________________________
There are two types of vessels, submarines and targets.

Top
#42499 - 2003-07-07 06:06 PM Re: Functions
David Nicholls Offline
Fresh Scripter

Registered: 2002-01-02
Posts: 24
Loc: London, UK
Took out the line
code:
Dim $udfInDomainGroup[1]

and tidied up so that it doesnt redim one extra time and it all works [Big Grin]

Thanks for your time again

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 781 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.059 seconds in which 0.023 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org