Page 1 of 1 1
Topic Options
#187577 - 2008-05-13 02:05 AM How do i return a array from a UDF?
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
Hello,

I'm having a bit of trouble grasping just how functions return values, namely arrays.

The below code does not work it when running the script, the function does not correctly return an array, why??:

 Code:
$abc = someudf(1)
For Each $element in $abc
	"element > " + $element
Next


Function someudf($somevar)
	Dim $someudf[4]
	Select
		Case $somevar = 1
			$someudf[0] = "someval"
			$someudf[1] = "someotherval"
			$someudf[2] = "anotherval"
			$someudf[3] = "yetanotherval"
			
		Case $somevar = 2
			$someudf[0] = "differentval"
			$someudf[1] = "differentvalagain"
			$someudf[2] = "anotherval"
			$someudf[3] = "yetanotherval"
					
	EndSelect
EndFunction


Thanks in advance,

Luke

Top
#187578 - 2008-05-13 02:39 AM Re: How do i return a array from a UDF? [Re: lukeod]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
Try this...

$abc = someudf(1)
For Each $element in $abc
"element > " + $element
Next




Function someudf($somevar)
Dim $somearray[4]
Select
Case $somevar = 1
$somearray[0] = "someval"
$somearray[1] = "someotherval"
$somearray[2] = "anotherval"
$somearray[3] = "yetanotherval"
$someudf=$somearray

Case $somevar = 2
$somearray[0] = "differentval"
$somearray[1] = "differentvalagain"
$somearray[2] = "anotherval"
$somearray[3] = "yetanotherval"
$someudf=$somearray

EndSelect
EndFunction


You really can't dim $someudf because it's the name of the UDF and is "automagically" dimmed by kixtart.
_________________________
(... better days ahead)

Top
#187580 - 2008-05-13 02:47 AM Re: How do i return a array from a UDF? [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
You could also just change this:
Dim $somearray[4]
to this:
Dim $somearray[3]

As I think you know, kixtart arrays are 0 based, so for 4 values, the value you would use is n-1 or 3 in this case.
_________________________
(... better days ahead)

Top
#187582 - 2008-05-13 02:54 AM Re: How do i return a array from a UDF? [Re: Allen]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
Ahh well that does make sense. I tried that and it worked. Also found the following worked the same way:

 Code:
$abc = someudf(1)
For Each $element in $abc
	"element > " + $element ?
Next


Function someudf($somevar)
	ReDim $someudf[3]
	Select
		Case $somevar = 1
			$someudf[0] = "someval"
			$someudf[1] = "someotherval"
			$someudf[2] = "anotherval"
			$someudf[3] = "yetanotherval"
			
		Case $somevar = 2
			$someudf[0] = "differentval"
			$someudf[1] = "differentvalagain"
			$someudf[2] = "anotherval"
			$someudf[3] = "yetanotherval"
					
	EndSelect
EndFunction


Which i suppose has the small advantage of taking one line of code out from each 'case', as the udf i'm making will have about 28 different cases. It's basically to pass the IP address of the machine to the UDF, which returns a bunch of site information.

Thanks for the help =D

Luke


Edited by lukeod (2008-05-13 02:57 AM)

Top
#187583 - 2008-05-13 02:57 AM Re: How do i return a array from a UDF? [Re: lukeod]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
Ahh yes, i noticed that small error when i viewed the output, had a null element. Cheers \:\)
Top
#187584 - 2008-05-13 02:59 AM Re: How do i return a array from a UDF? [Re: lukeod]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
While that may work, I would bet that is not good practice. Maybe Richard will comment on this...
_________________________
(... better days ahead)

Top
#187585 - 2008-05-13 03:08 AM Re: How do i return a array from a UDF? [Re: Allen]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4574
Loc: USA
Just saw your comments about the cases...

This would be a perfect use for ini files. Look for EnumINI() here on the board, and readprofilestring() and writeprofilestring() in the manual.
_________________________
(... better days ahead)

Top
#187587 - 2008-05-13 08:01 AM Re: How do i return a array from a UDF? [Re: Allen]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
or ad site-based scripts...
_________________________
!

download KiXnet

Top
#187588 - 2008-05-13 08:09 AM Re: How do i return a array from a UDF? [Re: Lonkero]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
instead of making scripts for all sites, could read the site from the global script... something like from technet't rip off:
 Code:
Set objADSysInfo = CreateObject("ADSystemInfo")

WScript.Echo "Current site name: " & objADSysInfo.SiteName
_________________________
!

download KiXnet

Top
#187620 - 2008-05-14 02:47 AM Re: How do i return a array from a UDF? [Re: Lonkero]
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11634
Loc: Space
And don't forget to drop out of the script right away when it can't read the SITE or some other similar issue or you'll be running the script even when you don't want to.
Top
#187908 - 2008-05-28 08:46 AM Re: How do i return a array from a UDF? [Re: NTDOC]
lukeod Offline
Getting the hang of it

Registered: 2008-01-11
Posts: 70
Loc: Australia
well i suppose i could use an ini file or such, but it's nice to keep it in one file for simplicity sake.

The reason i am scripting for every site is that we want to have a generic script which is identical for each site. The advantage being that in the fairly regular event of the script needing to change (for example, to change a local administrator password or the like) only one script needs to be modified and it will replicate to all the sites and it'll just work.

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
0 registered and 1046 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.062 seconds in which 0.024 seconds were spent on a total of 13 queries. Zlib compression enabled.

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