#181524 - 2007-10-11 07:08 PM
Adding Elements into Array from LDAP
|
werealldevo
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
|
|
|
|
#181529 - 2007-10-11 10:06 PM
Re: Adding Elements into Array from LDAP
[Re: DrillSergeant]
|
werealldevo
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
|
|
|
|
#181560 - 2007-10-12 04:15 PM
Re: Adding Elements into Array from LDAP
[Re: DrillSergeant]
|
werealldevo
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
|
|
|
|
#181565 - 2007-10-12 05:06 PM
Re: Adding Elements into Array from LDAP
[Re: Richard H.]
|
Richard H.
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
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
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
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
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
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!
|
|
Top
|
|
|
|
#181755 - 2007-10-18 03:34 PM
Re: Adding Elements into Array from LDAP
[Re: ChristopheM]
|
werealldevo
Fresh Scripter
Registered: 2007-10-03
Posts: 11
|
Thank you for the information. Great replies from everyone.
|
|
Top
|
|
|
|
Moderator: Shawn, ShaneEP, Ruud van Velsen, Arend_, Jochen, Radimus, Glenn Barnas, Allen, Mart
|
0 registered
and 580 anonymous users online.
|
|
|