Build your own Scripts to Manipulate LDIF Exports – Part 1



By: rkalfane

January 9, 2008 2:12 pm

Reads: 524

Comments:3

Rating:0

This entry is part 1 of 1 in the series Build your own Scripts to Manipulate LDIF Exports
  • Build your own Scripts to Manipulate LDIF Exports – Part 1

Table of Contents

Introduction

Using LDIFStruct_part1.zip

Introduction to Associative Arrays

Implementation in Each Language

        Php

        Perl

        Vim

        BeanShell

        Java

        Mono/C#

Summary


Introduction

This article follows the one about using a Vim script to generate backlinks LDIF from links export. The goal of this article is to help you create your own command-line scripts, so you can build a library to manipulate LDIF entries with your favorite language. Using associative arrays and lists, you can get a representation of LDIF entries that you can then manipulate in your scripts.

This article will show how to build a “LDIFStruct” library in several languages. The library will be able to load simple LDIF exports, merge LDIF exports, and display results as LDIF or XML. From there, you can adapt and improve the library to your needs, such as generating SQL statements or CSV exports.

The first part covers the following features:

  • Creating and accessing associative arrays
  • Creating and accessing lists
  • Looping through the different structures

These features will be explained using the following languages:

The second part of the article explains scripting basics, such as parsing command-line arguments, reading lines from a file, and using regular expression for each language.

The third part explains how to build the “LDIFStruct” library by combining all the features together. It also shows how to use the structures created from LDIF exports to generate XML, merged LDIF, etc.


Using LDIFStruct_part1.zip

Here is the content of the attached archive LDIFStruct_part1.zip that you can download:

./LDIFStruct
  +- /Php
  |  +- /test_arrays.php*
  +- /Vim
  |  +- /test_arrays.vim*
  |  +- /test_arrays.sh*
  +- /Java
  |  +- /TestArrays.java
  |  +- /TestArrays.class
  |  +- /TestArrays.jar
  |  +- /test_arrays.sh*
  +- /Mono
  |  +- /TestArrays.cs
  |  +- /TestArrays.exe*
  |  +- /test_arrays.sh*
  +- /Perl
  |  +- /test_arrays.pl*
  +- /BeanShell
     +- /test_arrays.bsh*
     +- /test_arrays.sh*
     +- /bsh-2.0b4.jar

All files named “test_arrays.*” are executable scripts demonstrating arrays and lists for each language. These scripts initialize arrays, add values, access arrays, loop through arrays and display the result on the standard output.


Introduction to Associative Arrays

An associative array is a data type composed of a collection of key/value pairs, where each key is associated with one value. Such array is also called map, hash, dictionary or lookup table. The operation of finding a value associated with a key is called a lookup: this is the most used and most important method when manipulating associative arrays.

A generic representation of such array could be the following:

myarray
{
  "jsmith" => "John Smith",
  "jdoe"   => "Jane Doe"
}

With this array, the value “John Smith” is associated with the key “jsmith” and the value “Jane Doe” is associated with the key “jdoe”. In that case, we have the following statements:

myarray[ "jsmith" ] = "John Smith"
myarray[ "jdoe" ]   = "Jane Doe"

It is also possible to use associative arrays as values. In that case you can have arrays of arrays. For instance:

myarray
{
  "jsmith" 
      => {
           "givenName" => "John",
           "sn"        => "Smith",
           "mail"      => "jsmith@mydomain1.com"
         },
  "jdoe"   
      => {
           "givenName" => "Jane",
           "sn"        => "Doe",
           "mail"      => "jdoe@mydomain1.com"
         }
}

The key “jsmith” is associated with another associative array containing “givenName”, “sn”, “mail” attributes as keys and their values. In that case, we have the following statements:

myarray[ "jsmith" ][ "givenName" ] = "John"
myarray[ "jsmith" ][ "sn" ]        = "Smith"
myarray[ "jsmith" ][ "mail" ]      = "jsmith@mydomain1.com"
myarray[ "jdoe" ][ "givenName" ]   = "Jane"
etc.

Let’s go a step further and handle multi-values for attributes. Instead of using “jsmith@mydomain1.com”, let’s use a list of values like the following:

[ "jsmith@mydomain1.com", "jsmith@mydomain2.com" ]

Using lists as values for attributes, we can finally end-up with a full structure handling attributes and their values for a set of users. For instance, let’s take the following LDIF export of two entries, “cn=jsmith,o=novell” and “cn=jdoe,o=novell”:

# LDIF export
version: 1

dn: cn=jsmith,o=novell
changetype: add
cn: jsmith
givenName: John
mail: jsmith@mydomain1.com
mail: jsmith@mydomain2.com
objectClass: inetOrgPerson
sn: Smith

dn: cn=jdoe,o=novell
changetype: add
cn: jdoe
givenName: Jane
mail: jdoe@mydomain1.com
mail: jdoe@mydomain2.com
objectClass: inetOrgPerson
sn: Doe

A representation of the content using associate arrays ans lists would be as the following:

myarray
{
  "cn=jsmith,o=novell" 
      => {
           "givenName"   => [ "John", "Henry" ],
           "sn"          => [ "Smith" ],
           "cn"          => [ "jsmith" ],
           "objectClass" => [ "inetOrgPerson" ],
           "mail"        => [ "jsmith@mydomain1.com", "jsmith@mydomain2.com" ]
         },
  "cn=jdoe,o=novell"   
      => {
           "givenName"   => [ "Jane", "Jennifer" ],
           "sn"          => [ "Doe" ],
           "cn"          => [ "jdoe" ],
           "objectClass" => [ "inetOrgPerson" ],
           "mail"        => [ "jdoe@mydomain1.com", "jdoe@mydomain2.com" ]
         }
}

In that case, we have the following statements:

myarray[ "jsmith" ][ "givenName" ][ 0 ] = "John"
myarray[ "jsmith" ][ "sn" ][ 0 ]        = "Smith"
myarray[ "jsmith" ][ "mail" ][ 0 ]      = "jsmith@mydomain1.com"
myarray[ "jsmith" ][ "mail" ][ 1 ]      = "jsmith@mydomain2.com"
myarray[ "jdoe" ][ "givenName" ][ 0 ]   = "Jane"
etc.

From that structure, you can easily re-build the LDIF export, or generate an XML document by looping entries, looping attributes for each entry and looping values for each attribute.

Now, let’s have a look at the objects to use for each of the languages.


Implementation in Each Language

Depending on the language, you can use Dictionary, Hashtable, HashMap and List, ArrayList objects. This section describe for each language the objects used to build the LDIFStruct utility library.

The result of all the test_arrays.* scripts demonstrating associative arrays and lists should be the following (myarray1 and myarray2 are normal arrays / lists and myarray3 is an associative array):

Sample values
myarray1:
Element #1 of myarray1 = Value1.1
Element #2 of myarray1 = Value1.2

myarray2:
Element #3 of myarray2 = Value2.3
Element #4 of myarray2 = Value2.4

myarray3:
Value for key1 in myarray3 = Value3.1
Value for key2 in myarray3 = Value3.2

Loops
Looping through myarray1:
Element #1 of myarray1 = Value1.1
Element #2 of myarray1 = Value1.2
Element #3 of myarray1 = Value1.3
Element #4 of myarray1 = Value1.4
Element #5 of myarray1 = Value1.5

Looping through myarray2:
Element #1 of myarray2 = Value2.1
Element #2 of myarray2 = Value2.2
Element #3 of myarray2 = Value2.3
Element #4 of myarray2 = Value2.4
Element #5 of myarray2 = Value2.5

Looping through myarray3:
Value for key1 in myarray3 = Value3.1
Value for key2 in myarray3 = Value3.2
Value for key3 in myarray3 = Value3.3
Value for key4 in myarray3 = Value3.4
Value for key5 in myarray3 = Value3.5

Php

In Php the same array data type can be used for associative arrays and lists. You can define a new array in Php using the following code:

 3 $myarray1 = array();
 4 
 5 $myarray2 = array( "Value2.1", "Value2.2", "Value2.3" );
 6 
 7 $myarray3 = array( "key1" => "Value3.1",
 8          "key2" => "Value3.2",
 9          "key3" => "Value3.3" );

Arrays $myarray2 and $myarray3 are created with data in them. You can add new entries in these arrays using the following code:

11 $myarray1[ 0 ] = "Value1.1";
12 $myarray1[ 1 ] = "Value1.2";
13 $myarray1[ 2 ] = "Value1.3";
14 $myarray1[ 3 ] = "Value1.4";
15 $myarray1[ 4 ] = "Value1.5";
16 
17 $myarray2[ 3 ] = "Value2.4";
18 $myarray2[ 4 ] = "Value2.5";
19 
20 $myarray3[ "key4" ] = "Value3.4";
21 $myarray3[ "key5" ] = "Value3.5";

You can also use the following functions to manipulate normal arrays:

  • array_push() to add elements onto the end of an array
  • array_pop() to retrieve and remove an element off the end of an array
  • array_shift() to retrieve and remove an element off the beginning of an array
  • array_unshift() to add elements at the beginning of an array

You can easily display elements from the arrays using the following:

25 echo( "Element #1 of myarray1 = ".$myarray1[ 0 ]."\n" );
26 echo( "Element #2 of myarray1 = ".$myarray1[ 1 ]."\n" );

29 echo( "Element #3 of myarray2 = ".$myarray2[ 2 ]."\n" );
30 echo( "Element #4 of myarray2 = ".$myarray2[ 3 ]."\n" );

33 echo( "Value for key1 in myarray3 = ".$myarray3[ "key1" ]."\n" );
34 echo( "Value for key2 in myarray3 = ".$myarray3[ "key2" ]."\n" );

Last, you can loop through values and key / value pairs using the following code:

39 $index = 1;
40 foreach( $myarray1 as $value )
41 {
42   echo( "Element #".$index." of myarray1 = ".$value."\n" );
43   $index++;
44 }

47 $index = 1;
48 foreach( $myarray2 as $value )
49 {
50   echo( "Element #".$index." of myarray2 = ".$value."\n" );
51   $index++;
52 }

55 $keys = array_keys( $myarray3 );
56 asort( $keys );
57 foreach( $keys as $key )
58 {
59   echo( "Value for ".$key." in myarray3 = ".$myarray3[ $key ]."\n" );
60 }

When executing the test_arrays code, that you can find in the file “test_arrays.php”, the result should be as described at the beginning of the section.

Once in the ./LDIFStruct/Php directory, you can call the script test_arrays.php:

/LDIFStruct/Php> ./test_arrays.php

The script has to be executable. You can make it executable if it is not using the following command in Linux:

/LDIFStruct/Php> chmod +x test_arrays.php

If it doesn’t work calling directly test_arrays.php, you can also call the php executable with test_arrays.php as argument. For instance, on my Linux box with Php5 installed:

/LDIFStruct/Php> /usr/bin/php5 test_arrays.php

or on a Windows box, if php.exe is in the execution path:

C:\LDIFStruct\Php\> php.exe test_arrays.php

If you want to store the result in a text file to better read it, redirect the output:

/LDIFStruct/Php> ./test_arrays.php > test_arrays.log

Perl

In Perl, there are two types of arrays: normal arrays and associative arrays. You can define these arrays in Perl using the following code (use “@” for normal arrays and “%” for associative arrays):

 3 @myarray1 = ();
 4 
 5 @myarray2 = ( "Value2.1", "Value2.2", "Value2.3" );
 6 
 7 %myarray3 = ( "key1" => "Value3.1",
 8     "key2" => "Value3.2",
 9     "key3" => "Value3.3" );

Arrays accessible via the variables $myarray2 and $myarray3 are created with data in them. You can add new entries in these arrays using the following code (use brackets “[", "]“, for arrays and braces “{“, “}”, for associative arrays):

11 $myarray1[ 0 ] = "Value1.1";
12 $myarray1[ 1 ] = "Value1.2";
13 $myarray1[ 2 ] = "Value1.3";
14 $myarray1[ 3 ] = "Value1.4";
15 $myarray1[ 4 ] = "Value1.5";
16 
17 $myarray2[ 3 ] = "Value2.4";
18 $myarray2[ 4 ] = "Value2.5";
19 
20 $myarray3{ "key4" } = "Value3.4";
21 $myarray3{ "key5" } = "Value3.5";

You can also use the following functions to manipulate normal arrays:

  • push() to add elements onto the end of an array
  • pop() to retrieve and remove an element off the end of an array
  • shift() to retrieve and remove an element off the beginning of an array
  • unshift() to add elements at the beginning of an array

You can easily display elements from the arrays using the following:

25 print( "Element #1 of myarray1 = ".$myarray1[ 0 ]."\n" );
26 print( "Element #2 of myarray1 = ".$myarray1[ 1 ]."\n" );

29 print( "Element #3 of myarray2 = ".$myarray2[ 2 ]."\n" );
30 print( "Element #4 of myarray2 = ".$myarray2[ 3 ]."\n" );

33 print( "Value for key1 in myarray3 = ".$myarray3{ "key1" }."\n" );
34 print( "Value for key2 in myarray3 = ".$myarray3{ "key2" }."\n" );

Last, you can loop through values and key / value pairs using the following code:

39 $index = 1;
40 foreach my $value ( @myarray1 )
41 {
42   print( "Element #".$index." of myarray1 = ".$value."\n" );
43   $index++;
44 }

47 $index = 1;
48 foreach my $value ( @myarray2 )
49 {
50   print( "Element #".$index." of myarray2 = ".$value."\n" );
51   $index++;
52 }

55 foreach my $key ( sort keys %myarray3 )
56 {
57   print( "Value for ".$key." in myarray3 = ".$myarray3{ $key }."\n" );
58 }

Once in the ./LDIFStruct/Perl directory, you can call the script test_arrays.pl:

/LDIFStruct/Perl> ./test_arrays.pl

The script has to be executable. You can make it executable if it is not using the following command in Linux:

/LDIFStruct/Perl> chmod +x test_arrays.pl

If you want to store the result in a text file to better read it, redirect the output:

/LDIFStruct/Perl> ./test_arrays.pl > test_arrays.log

Vim

In Vim you can use the “Dictionary” type for associative arrays and the “List” type for normal lists. You can define these objects in Vim using the following code (the code is simplier than for other languages; use brackets “[", "]“, for lists and braces “{“, “}”, for associative arrays):

 1 let myarray1 = []
 2 
 3 let myarray2 = [ "Value2.1", "Value2.2", "Value2.3" ]
 4 
 5 let myarray3 = { "key1": "Value3.1", "key2": "Value3.2", "key3": "Value3.3" }

Arrays myarray2 and myarray3 are created with data in them. You can add new entries in these arrays using the following code:

 7 call add( myarray1, "Value1.1" )
 8 call add( myarray1, "Value1.2" )
 9 call add( myarray1, "Value1.3" )
10 call add( myarray1, "Value1.4" )
11 call add( myarray1, "Value1.5" )
12 
13 call add( myarray2, "Value2.4" )
14 call add( myarray2, "Value2.5" )
15 
16 let myarray3[ "key4" ] = "Value3.4"
17 let myarray3[ "key5" ] = "Value3.5"

You can easily display elements from the arrays using the following:

21 echo "Element #1 of myarray1 = ".myarray1[ 0 ]
22 echo "Element #2 of myarray1 = ".myarray1[ 1 ]

25 echo "Element #3 of myarray2 = ".myarray2[ 2 ]
26 echo "Element #4 of myarray2 = ".myarray2[ 3 ]

29 echo "Value for key1 in myarray3 = ".myarray3[ "key1" ]
30 echo "Value for key2 in myarray3 = ".myarray3[ "key2" ]

Last, you can loop through values and key / value pairs using the following code (the code is simplier than for other languages):

35 let index = 1
36 for value in myarray1
37   echo "Element #".index." of myarray1 = ".value
38   let index = index + 1
39 endfor

42 let index = 1
43 for value in myarray2
44   echo "Element #".index." of myarray2 = ".value
45   let index = index + 1
46 endfor

49 for key in sort( keys( myarray3 ) )
50   echo "Value for ".key." in myarray3 = ".myarray3[ key ]
51 endfor

Once in the ./LDIFStruct/Vim directory, you can call the scripts test_arrays.vim or test_arrays.sh:

/LDIFStruct/Vim> ./test_arrays.vim
or
/LDIFStruct/Vim> ./test_arrays.sh

The scripts have to be executable. You can make them executable if they are not using the following commands in Linux:

/LDIFStruct/Vim> chmod +x test_arrays.vim
/LDIFStruct/Vim> chmod +x test_arrays.sh

The result will be displayed in a Vim buffer as following:

BeanShell

BeanShell is a small Java source intepreter convenient to use because you can use variables without types, without class casts, loops are easy to use, there are simplified calls to object methods, etc. It provides both the power of Java APIs and the ease of a scripting language. Check it out!

In BeanShell -aka Java- you can use HashMap or HashTable objects for associative arrays (or even TreeMap for sorted keys arrays) and ArrayList objects (better than Vector objects) for lists. You can define these structures using the following code:

 3 myarray1 = new ArrayList();
 4 
 5 myarray2 = new ArrayList();
 6 myarray2.add( "Value2.1" );
 7 myarray2.add( "Value2.2" );
 8 myarray2.add( "Value2.3" );
 9 
10 myarray3 = new HashMap();
11 myarray3.put( "key1", "Value3.1" );
12 myarray3.put( "key2", "Value3.2" );
13 myarray3.put( "key3", "Value3.3" );

Arrays myarray2 and myarray3 are created with data in them. You can add new entries in these arrays using the following code:

15 myarray1.add( "Value1.1" );
16 myarray1.add( "Value1.2" );
17 myarray1.add( "Value1.3" );
18 myarray1.add( "Value1.4" );
19 myarray1.add( "Value1.5" );
20 
21 myarray2.add( "Value2.4" );
22 myarray2.add( "Value2.5" );
23 
24 myarray3.put( "key4", "Value3.4" );
25 myarray3.put( "key5", "Value3.5" );

You can easily display elements from the arrays using the following:

29 print( "Element #1 of myarray1 = " + myarray1.get( 0 ) );
30 print( "Element #2 of myarray1 = " + myarray1.get( 1 ) );

33 print( "Element #3 of myarray2 = " + myarray2.get( 2 ) );
34 print( "Element #4 of myarray2 = " + myarray2.get( 3 ) );

37 print( "Value for key1 in myarray3 = " + myarray3.get( "key1" ) );
38 print( "Value for key2 in myarray3 = " + myarray3.get( "key2" ) );

Last, you can loop through values and key / value pairs using the following code (sorting can be a bit more complex than other languages if you don’t use TreeMap):

43 index = 1;
44 for( value : myarray1 )
45 {
46   print( "Element #" + index + " of myarray1 = " + value );
47   index++;
48 }

51 index = 1;
52 for( value : myarray2 )
53 {
54   print( "Element #" + index + " of myarray2 = " + value );
55   index++;
56 }

59 keys = new ArrayList( myarray3.keySet() );
60 Collections.sort( keys );
61 for( key : keys )
62 {
63   print( "Value for " + key + " in myarray3 = " + myarray3.get( key ) );
64 }

Once in the ./LDIFStruct/BeanShell directory, you can call the scripts test_arrays.sh or test_arrays.bsh:

/LDIFStruct/BeanShell> ./test_arrays.sh
or
/LDIFStruct/BeanShell> ./test_arrays.bsh

The scripts have to be executable. You can make them executable if they are not using the following commands in Linux:

/LDIFStruct/BeanShell> chmod +x test_arrays.sh
/LDIFStruct/BeanShell> chmod +x test_arrays.bsh

The test_arrays.sh script call a Java command with the file bsh-2.0b4.jar in the classpath and the file test-arrays.bsh as argument.

 1 #!/bin/sh
 2 
 3 export CLASSPATH=bsh-2.0b4.jar
 4 ./test_arrays.bsh

You can call the test_arrays.bsh script directly if you edit it to set your correct java binary path and if this file bsh-2.0b4.jar is in the classpath. You can add this file in your classpath on Linux by editing your .bashrc file for instance and add the following line:

export CLASSPATH=/path/to/your/file/bsh-2.0b4.jar

If you want to store the result in a text file to better read it, redirect the output:

/LDIFStruct/BeanShell> ./test_arrays.sh > test_arrays.log
or
/LDIFStruct/BeanShell> ./test_arrays.bsh > test_arrays.log

Java

In Java as for BeanShell you can use HashMap or HashTable objects for associative arrays (or even TreeMap for sorted keys arrays) and ArrayList objects (better than Vector objects) for lists. You can define these structures using the following code (typing of variables is strict):

 7     ArrayList myarray1 = new ArrayList();
 8 
 9     ArrayList myarray2 = new ArrayList();
10     myarray2.add( "Value2.1" );
11     myarray2.add( "Value2.2" );
12     myarray2.add( "Value2.3" );
13 
14     HashMap myarray3 = new HashMap();
15     myarray3.put( "key1", "Value3.1" );
16     myarray3.put( "key2", "Value3.2" );
17     myarray3.put( "key3", "Value3.3" );

Arrays myarray2 and myarray3 are created with data in them. You can add new entries in these arrays using the following code:

19     myarray1.add( "Value1.1" );
20     myarray1.add( "Value1.2" );
21     myarray1.add( "Value1.3" );
22     myarray1.add( "Value1.4" );
23     myarray1.add( "Value1.5" );
24 
25     myarray2.add( "Value2.4" );
26     myarray2.add( "Value2.5" );
27 
28     myarray3.put( "key4", "Value3.4" );
29     myarray3.put( "key5", "Value3.5" );

You can easily display elements from the arrays using the following:

33     System.out.println( "Element #1 of myarray1 = " + myarray1.get( 0 ) );
34     System.out.println( "Element #2 of myarray1 = " + myarray1.get( 1 ) );

37     System.out.println( "Element #3 of myarray2 = " + myarray2.get( 2 ) );
38     System.out.println( "Element #4 of myarray2 = " + myarray2.get( 3 ) );

41     System.out.println( "Value for key1 in myarray3 = " + myarray3.get( "key1" ) );
42     System.out.println( "Value for key2 in myarray3 = " + myarray3.get( "key2" ) );

Last, you can loop through values and key / value pairs using the following code (again, sorting is a bit more complex without TreeMap use):

47     int index = 1;
48     Iterator myarray1Iterator = myarray1.iterator();
49     while( myarray1Iterator.hasNext() )
50     {
51       String value = (String) myarray1Iterator.next();
52       System.out.println( "Element #" + index + " of myarray1 = " + value );
53       index++;
54     }

57     index = 1;
58     Iterator myarray2Iterator = myarray2.iterator();
59     while( myarray2Iterator.hasNext() )
60     {
61       String value = (String) myarray2Iterator.next();
62       System.out.println( "Element #" + index + " of myarray2 = " + value );
63       index++;
64     }

67     ArrayList keys = new ArrayList( myarray3.keySet() );
68     Collections.sort( keys );
69     Iterator keysIterator = keys.iterator();
70     while( keysIterator.hasNext() )
71     {
72       String key = (String) keysIterator.next();
73       System.out.println( "Value for " + key + " in myarray3 = " + myarray3.get( key ) );
74     }

Once in the ./LDIFStruct/Java directory, you can call the script test_arrays.sh:

/LDIFStruct/Java> ./test_arrays.sh

The script has to be executable. You can make it executable if it is not using the following command in Linux:

/LDIFStruct/Java> chmod +x test_arrays.sh

If you want to store the result in a text file to better read it, redirect the output:

/LDIFStruct/Java> ./test_arrays.sh > test_arrays.log

If you change the TestArrays.java source code, here are the commands to use to compile and rebuild the TestArrays.jar file (you can then execute the test_arrays.sh script):

/LDIFStruct/Java> javac TestArrays.java
/LDIFStruct/Java> jar cvf TestArrays.jar TestArrays.class
added manifest
adding: TestArrays.class(in = 2826) (out= 1385)(deflated 50%)

Mono/C#

Mono allows you to develop and run .NET applications on multiple platforms (Linux, Solaris, Max OS X, Windows and Unix) and also includes a C# compiler. It is sponsored by Novell, so check it out!

In Mono/C#, a bit like in Java, you can use Hashtable objects for associative arrays and ArrayList objects for lists. There should also be (soon or already in) a Dictionary object which looks like an improvment or the Hashtable object. You can define these structures using the following code:

 8     ArrayList myarray1 = new ArrayList();
 9 
10     ArrayList myarray2 = new ArrayList();
11     myarray2.Add( "Value2.1" );
12     myarray2.Add( "Value2.2" );
13     myarray2.Add( "Value2.3" );
14 
15     Hashtable myarray3 = new Hashtable();
16     myarray3.Add( "key1", "Value3.1" );
17     myarray3.Add( "key2", "Value3.2" );
18     myarray3.Add( "key3", "Value3.3" );

Arrays myarray2 and myarray3 are created with data in them. You can add new entries in these arrays using the following code (note the similarity with Java syntax):

20     myarray1.Add( "Value1.1" );
21     myarray1.Add( "Value1.2" );
22     myarray1.Add( "Value1.3" );
23     myarray1.Add( "Value1.4" );
24     myarray1.Add( "Value1.5" );
25 
26     myarray2.Add( "Value2.4" );
27     myarray2.Add( "Value2.5" );
28 
29     myarray3.Add( "key4", "Value3.4" );
30     myarray3.Add( "key5", "Value3.5" );

You can easily display elements from the arrays using the following (accessing values is simplier than in Java as using the array notation with brackets “[", "]“):

34     Console.WriteLine( "Element #1 of myarray1 = " + myarray1[ 0 ] );
35     Console.WriteLine( "Element #2 of myarray1 = " + myarray1[ 1 ] );

38     Console.WriteLine( "Element #3 of myarray2 = " + myarray2[ 2 ] );
39     Console.WriteLine( "Element #4 of myarray2 = " + myarray2[ 3 ] );

42     Console.WriteLine( "Value for key1 in myarray3 = " + myarray3[ "key1" ] );
43     Console.WriteLine( "Value for key2 in myarray3 = " + myarray3[ "key2" ] );

Last, you can loop through values and key / value pairs using the following code:

48     int index = 1;
49     foreach( string value in myarray1 )
50     {
51       Console.WriteLine( "Element #" + index + " of myarray1 = " + value );
52       index++;
53     }

56     index = 1;
57     foreach( string value in myarray2 )
58     {
59       Console.WriteLine( "Element #" + index + " of myarray2 = " + value );
60       index++;
61     }

64     ArrayList keys = new ArrayList( myarray3.Keys );
65     keys.Sort();
66     foreach( string key in keys )
67     {
68       Console.WriteLine( "Value for " + key + " in myarray3 = " + myarray3[ key ] );
69     }

Once in the ./LDIFStruct/Mono directory, you can call the script test_arrays.sh:

/LDIFStruct/Mono> ./test_arrays.sh

The script has to be executable. You can make it executable if it is not using the following command in Linux:

/LDIFStruct/Mono> chmod +x test_arrays.sh

If you want to store the result in a text file to better read it, redirect the output:

/LDIFStruct/Mono> ./test_arrays.sh > test_arrays.log

If you change the TestArrays.cs source code, here is the command to use to compile and rebuild the TestArrays.exe file (you can then execute the test_arrays.sh script):

/LDIFStruct/Java> mcs TestArrays.cs


Summary

You had a quick overview about associative arrays and lists, their implementation and their use in different languages (sorry, I didn’t use VBScript as I am working on a nice SLED machine…). You can use these objects to easily build data structures, which will be interesting for us to load LDIF exports.

Before building the final library to manipulate LDIF entries, you will see in the next part the basics of scripts programmation: parsing the command-line arguments, reading from a file and using regular expressions. From there, you will have the base to develop your own scripts using the language of your choice… Stay tuned!

VN:F [1.9.22_1171]
Rating: 0.0/5 (0 votes cast)

Tags: , ,
Categories: Identity Manager, Technical Solutions

Disclaimer: As with everything else at NetIQ Cool Solutions, this content is definitely not supported by NetIQ, so Customer Support will not be able to help you if it has any adverse effect on your environment.  It just worked for at least one person, and perhaps it will be useful for you too.  Be sure to test in a non-production environment.

3 Comments

  1. By:RFBonte

    Nice overview of all kind of languages, but I miss Visual Basic. VBA is extra interesting because a lot of Identity Manipulations are done in Excel. And since for eDirectory you can get your data with the ODBC driver, the combination is gold.

    Nice VBA macro to convert excel to ldif
    http://www.faq-o-matic.net/2006/06/04/ldif-generator-fuer-den-import-in-active-directory/

    VBA Macro to convert DN syntax to LDAP
    input DN2LDAP(“user1.dep1.dep.org”)
    output “cn=user1,ou=dep1,ou=dep,o=org”
    Public Function DN2LDAP(inContext As String) As String
    Dim iPosOUBeg As Integer
    Dim iPosOUEnd As Integer
    Dim iTmp As Integer
    Dim strO As String
    Dim strCN As String
    Dim strOU As String
    Dim strContext As String

    ‘ determine cn part
    iTmp = InStr(inContext, “.”)
    strCN = “cn=” & Left(inContext, (iTmp – 1))
    iPosOUBeg = iTmp + 1

    ‘ determine o part
    iTmp = InStrRev(inContext, “.”)
    strO = “,o=” & Mid(inContext, (iTmp + 1))
    iPosOUEnd = iTmp – 1

    ‘ Transform all ou parts
    strOU = “”
    strContext = Mid(inContext, iPosOUBeg, (iPosOUEnd – iPosOUBeg) + 1)

    ‘Find first “.”in ou part
    iTmp = InStr(strContext, “.”)

    ‘do as long as there is a “.”
    Do While iTmp > 0
    strOU = strOU & “,ou=” & Left(strContext, (iTmp – 1))
    strContext = Mid(strContext, iTmp + 1)
    iTmp = InStr(strContext, “.”)
    Loop
    ‘add the last ou part
    strOU = strOU & “,ou=” & strContext

    DN2LDAP = strCN & strOU & strO

    End Function

  2. By:rkalfane

    Hello!

    Thanks for your comments. You are right, VBScript could be very useful in our projects and I have already used it to modify registry entries for instance.

    Here is the “test_arrays.vbs” VBScript version for this first part of the article (it uses normal arrays and Dictionary objects for associative arrays):

    ' Initialize arrays
    Dim myarray1(4)
    
    Dim myarray2(4)
    myarray2(0) = "Value2.1"
    myarray2(1) = "Value2.2"
    myarray2(2) = "Value2.3"
    
    Set myarray3 = CreateObject("Scripting.Dictionary")
    myarray3.Add "key1", "Value3.1"
    myarray3.Add "key2", "Value3.2"
    myarray3.Add "key3", "Value3.3"
    
    ' Add values
    myarray1(0) = "Value1.1"
    myarray1(1) = "Value1.2"
    myarray1(2) = "Value1.3"
    myarray1(3) = "Value1.4"
    myarray1(4) = "Value1.5"
    
    myarray2(3) = "Value2.4"
    myarray2(4) = "Value2.5"
    
    myarray3.Add "key4", "Value3.4"
    myarray3.Add "key5", "Value3.5"
    
    ' Access arrays
    WScript.Echo "Sample values"
    WScript.Echo "myarray1:"
    WScript.Echo "Element #1 of myarray1 = " & myarray1( 0 )
    WScript.Echo "Element #2 of myarray1 = " & myarray1( 1 )
    WScript.Echo ""
    WScript.Echo "myarray2:"
    WScript.Echo "Element #3 of myarray2 = " & myarray2( 2 )
    WScript.Echo "Element #4 of myarray2 = " & myarray2( 3 )
    WScript.Echo ""
    WScript.Echo "myarray3:"
    WScript.Echo "Value for key1 in myarray3 = " & myarray3.Item( "key1" )
    WScript.Echo "Value for key2 in myarray3 = " & myarray3.Item( "key2" )
    WScript.Echo ""
    
    ' Loops through arrays
    WScript.Echo "Loops"
    WScript.Echo "Looping through myarray1:"
    Dim count
    count = 1
    For Each item In myarray1
    	WScript.Echo "Element #" & count & " of myarray1 = " & item
    	count = count + 1
    Next
    WScript.Echo ""
    
    WScript.Echo "Looping through myarray2:"
    count = 1
    For Each item In myarray2
    	WScript.Echo "Element #" & count & " of myarray2 = " & item
    	count = count + 1
    Next
    WScript.Echo ""
    
    WScript.Echo "Looping through myarray3:"
    For Each key in myarray3.Keys
    	WScript.Echo "Value for " & key & " in myarray3 = " & myarray3.Item( key )
    Next
    

    And to execute it:

    C:\LDIFStruct\VBScript\> cscript test_arrays.vbs
    

    Cheers!

    Reza

  3. By:rkalfane

    Hello,

    I also forgot one I used a long time ago, which has the simplicity of Vim Script in the notation and in the function calls, and that can be very powerful: Python!

    Hence, here is the Python version of this first part:

     1 #!/usr/bin/python
     2 myarray1 = [] 
     3 
     4 myarray2 = [ "Value2.1", "Value2.2", "Value2.3" ] 
     5 
     6 myarray3 = { "key1": "Value3.1",
     7        "key2": "Value3.2",
     8        "key3": "Value3.3" }
     9 
    10 # Add values
    11 myarray1.append( "Value1.1" )
    12 myarray1.append( "Value1.2" )
    13 myarray1.append( "Value1.3" )
    14 myarray1.append( "Value1.4" )
    15 myarray1.append( "Value1.5" )
    16 
    17 myarray2.append( "Value2.4" )
    18 myarray2.append( "Value2.5" )
    19 
    20 myarray3[ "key4" ] = "Value3.4"
    21 myarray3[ "key5" ] = "Value3.5"
    22 
    23 # Access arrays
    24 print "Sample values"
    25 print "myarray1:"
    26 print "Element #1 of myarray1 = " + myarray1[ 0 ]
    27 print "Element #2 of myarray1 = " + myarray1[ 1 ]
    28 print ""
    29 print "myarray2:"
    30 print "Element #3 of myarray2 = " + myarray2[ 2 ]
    31 print "Element #4 of myarray2 = " + myarray2[ 3 ]
    32 print ""
    33 print "myarray3:"
    34 print "Value for key1 in myarray3 = " + myarray3[ "key1" ]
    35 print "Value for key2 in myarray3 = " + myarray3[ "key2" ]
    36 print ""
    37 
    38 # Loops through arrays
    39 print "Loops"
    40 print "Looping through myarray1:"
    41 index = 1
    42 for value in myarray1:
    43   print "Element #" + str(index) + " of myarray1 = " + value
    44   index = index + 1
    45 
    46 print ""
    47 print "Looping through myarray2:"
    48 index = 1
    49 for value in myarray2:
    50   print "Element #" + str(index) + " of myarray2 = " + value
    51   index = index + 1
    52 
    53 print ""
    54 print "Looping through myarray3:"
    55 for key in sorted( myarray3 ):
    56   print "Value for " + key + " in myarray3 = " + myarray3[ key ]
    

    To make it executable:

    /LDIFStruct/Python> chmod +x test_arrays.py
    

    To execute it:

    /LDIFStruct/Python> ./test_arrays.py
    

    Cheers,

    Reza

Comment