Page 1 of 1 1
Topic Options
#181524 - 2007-10-11 07:08 PM Adding Elements into Array from LDAP
werealldevo Offline
Fresh Scripter

Registered: 2007-10-03
Posts: 11
Hello All,

I am attempting to write a function which will attach to a group and all of it's members(that are computers) to an array.

Attaching to the group and parsing for computers works fine... however, I do not know how to add all of these elements into an array using kixtart.

Would someone please offer some advice.

Thanks

=====code=====

Function enumComps()
$objADsPath = GetObject("LDAP://CN=,DC=,DC=)

For Each $obj in $objADsPath.Members

if $obj.Class = "computer"
;array here

endif
Next
EndFunction

Top
#181526 - 2007-10-11 09:23 PM Re: Adding Elements into Array from LDAP [Re: werealldevo]
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
This is one way but there are many others...

How many computers are we talking about?

 Code:
Function enumComps() 

$objADsPath = GetObject("LDAP://CN=,DC=,DC=) 

For Each $obj in $objADsPath.Members 
	If $obj.Class = "computer"
		; add each computer to a string
		$strComputers = $strComputers + $obj.name + ";"
	Endif 
Next 

If Len($strComputers)>0
	; take the last ; off the string
	$strComputers = Substr($strComputers,1,Len($strComputers)-1)
	
	; split the string into an array and return it
 	$enumComps = Split($strComputers,";")
EndIf

EndFunction 
_________________________
The Code is out there

Top
#181529 - 2007-10-11 10:06 PM Re: Adding Elements into Array from LDAP [Re: DrillSergeant]
werealldevo Offline
Fresh Scripter

Registered: 2007-10-03
Posts: 11
200+ computers. However, if you query for "obj.Cn" opposed to "obj.name" it will return for the netbios computer name. I guess it will be possible to trim down some of the code. Is this an inefficient way to create an array?
Top
#181530 - 2007-10-11 10:09 PM Re: Adding Elements into Array from LDAP [Re: werealldevo]
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
For that amount of computers this is an acceptable way.
_________________________
The Code is out there

Top
#181560 - 2007-10-12 04:15 PM Re: Adding Elements into Array from LDAP [Re: DrillSergeant]
werealldevo Offline
Fresh Scripter

Registered: 2007-10-03
Posts: 11
How would I create an array with an undefined set of elements, and add each computer's name to it within the first For Next control?

For example:

If I used "obj.CN" the script will return an unadorned name, "Computer1".
I would like to create an array and add each computer name as the next element. In java you could do something like this:

//=====JAVA=================
for (int i = 0; i < a.length; i++) { //add user data to array (currently 8 elements)
String strA = keybd.readLine();
a[i] = Integer.parseInt(strA);
}

This adds each piece of user data into the array:

+---+---+---+---+---+---+---+---+
| 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
+---+---+---+---+---+---+---+---+

We start at idx 0 and continue adding elements into the array... then you can simple parse the output:

System.out.println("SORTED ARRAY VALUES:");
for (int i = 0; i < a.length; i++) {
System.out.println("a["+i+"] = " + a[i]);
}

//=====END JAVA=================

Is there a way to do this in Kixtart? Perhaps using the Ubound Function ?

Top
#181561 - 2007-10-12 04:41 PM Re: Adding Elements into Array from LDAP [Re: werealldevo]
DrillSergeant Offline
MM club member
*****

Registered: 2004-07-09
Posts: 1164
Loc: Eijsden, the Netherlands
 Code:
Dim $MyArray[]

For $i = 0 to 50
	ReDim Preserve $MyArray[UBound($MyArray)+1]
	$MyArray[UBound($MyArray)] = 'this is value ' + $i
Next


You can do this, but it is slower than the first option I gave you...
_________________________
The Code is out there

Top
#181564 - 2007-10-12 04:50 PM Re: Adding Elements into Array from LDAP [Re: DrillSergeant]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Yup, just spotted that and deleted the post. This thread is sure gonna look strange now!
Top
#181565 - 2007-10-12 05:06 PM Re: Adding Elements into Array from LDAP [Re: Richard H.]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
You can wrap the array extension up into a simple UDF, which makes it a bit tidier:


$MyArray=ExtendArray($MyArray,"One")
$MyArray=ExtendArray($MyArray,"two")
$MyArray=ExtendArray($MyArray,"three")

For Each $s In $MyArray
"Array element is: "+$s+@CRLF
Next

Function ExtendArray($a,$v)
Redim Preserve $a[UBound($a)+1]
$a[UBound($a)]=$v
$ExtendArray=$a
EndFunction

Top
#181566 - 2007-10-12 05:07 PM Re: Adding Elements into Array from LDAP [Re: DrillSergeant]
werealldevo Offline
Fresh Scripter

Registered: 2007-10-03
Posts: 11
Thank you for your help!

This also works well. Why do you say this operation(option two) is slower--and how can we reliably test that assertion? It was my thought that option two would be faster, as there are less operations. Please clarify.

Again, thank you for your help-- just what I was looking for.


code:

Call enumComps

For Each $Item in $MyArray
?$Item
Next

Function enumComps()
Global $MyArray[]
$objADsPath = GetObject("LDAP://CN=,OU=,DC=,DC=")
For Each $obj in $objADsPath.Members
If $obj.Class = "computer"
ReDim Preserve $MyArray[UBound($MyArray)+1]
$MyArray[UBound($MyArray)] = $obj.cn
EndIf
Next


Edited by werealldevo (2007-10-12 05:08 PM)

Top
#181572 - 2007-10-12 10:02 PM Re: Adding Elements into Array from LDAP [Re: werealldevo]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
Consider the following methods of array processing:

Assume that $List contains an unknown number of items, up to 100. We want to put them into an array that's exactly the right size, between 1 and 100 elements.

METHOD 1
 Code:
Dim $X[99], $P
$P = -1
For Each $E in $list
  $P=$P+1
  $X[$P]=$E
Next
ReDim Preserve $X[$P]

Only one ReDim, no UBound command, requires pointer for target array

METHOD2
 Code:
Dim $X[], $P
$P = -1
For Each $E in $list
  $P=$P+1
  ReDim Preserve $X[$P]
  $X[$P]=$E
Next

One ReDim commands per item, no UBound command, requires pointer for target array

METHOD 3
 Code:
Dim $X[]
For Each $E in $list
  ReDim Preserve $X[UBound($X)+1]
  $X[UBound($X)]=$E
Next

One ReDim and two UBound commands per item, no pointer needed. This code is very tight and self-managing, but requires more processing than either of the other two methods.

None is either right or wrong, but #1 might be better suited for systems with lots of memory or very large arrays. I say this because the larger the array becomes, the more times you will need to call ReDim and/or UBound functions, resulting in increased overhead.

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#181602 - 2007-10-14 09:10 PM Re: Adding Elements into Array from LDAP [Re: Glenn Barnas]
ChristopheM Offline
Hey THIS is FUN
*****

Registered: 2002-05-13
Posts: 311
Loc: STRASBOURG, France
i suggest two other methods.

Method 4, arraySize is multiplied by 2 when arrayIndex exceeds arraySize.
 Code:
Dim $arraySize	$arraySize=256	; minimum value must be 1
Dim $array[$arraySize]
Dim $arrayIndex	$arrayIndex = -1

For Each $E in $list
	$arrayIndex = $arrayIndex + 1
	if $arrayIndex > $arraySize
		$arraySize = $arraySize * 2
		redim preserve $array[$arraySize]
	endif

	$array[$arrayIndex]=$E
Next

if $arrayIndex=-1
	; empty array
	$array = nothing
else
	redim preserve $array[$arrayIndex]
endif


Method 5, arraySize is incremented with arrayInc when arrayIndex exceeds arraySize.
 Code:
Dim $arrayInc	$arrayInc=256		; minimum value must be 1
Dim $arraySize	$arraySize=$arrayInc
Dim $array[$arraySize]
Dim $arrayIndex	$arrayIndex = -1

For Each $E in $list
	$arrayIndex = $arrayIndex + 1
	if $arrayIndex > $arraySize
		$arraySize = $arraySize +$arrayInc
		redim preserve $array[$arraySize]
	endif

	$array[$arrayIndex]=$E
Next

if $arrayIndex=-1
	; empty array
	$array = nothing
else
	redim preserve $array[$arrayIndex]
endif


Christophe
(sorry for my english)
_________________________
Christophe

Top
#181755 - 2007-10-18 03:34 PM Re: Adding Elements into Array from LDAP [Re: ChristopheM]
werealldevo Offline
Fresh Scripter

Registered: 2007-10-03
Posts: 11
Thank you for the information. Great replies from everyone.
Top
Page 1 of 1 1


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

Who's Online
0 registered and 580 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.063 seconds in which 0.025 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