Array of Hashes
-
I want to write a subroutine which fetches data from a database, save column to a hashtable (columname=columnvalue) and then put each dataset hashtable into an array ... then i want to return this array to the main program and run through each element and dereference the element back to a hashtable ... in perl i know how to do this - but have no clue how this had to be writen in c# ... (perl: %hash = %$array[element]) i trie something to put a hashtable into an array of string[] but i don't know how to get the hash back from the element of the string[] ... Thanks for your help! Matthias
-
I want to write a subroutine which fetches data from a database, save column to a hashtable (columname=columnvalue) and then put each dataset hashtable into an array ... then i want to return this array to the main program and run through each element and dereference the element back to a hashtable ... in perl i know how to do this - but have no clue how this had to be writen in c# ... (perl: %hash = %$array[element]) i trie something to put a hashtable into an array of string[] but i don't know how to get the hash back from the element of the string[] ... Thanks for your help! Matthias
-
I want to write a subroutine which fetches data from a database, save column to a hashtable (columname=columnvalue) and then put each dataset hashtable into an array ... then i want to return this array to the main program and run through each element and dereference the element back to a hashtable ... in perl i know how to do this - but have no clue how this had to be writen in c# ... (perl: %hash = %$array[element]) i trie something to put a hashtable into an array of string[] but i don't know how to get the hash back from the element of the string[] ... Thanks for your help! Matthias
//Create array of hashtables HashTable[] ha = new HashTable[number_of_elements_in_array](); //create a hashtable and populate it HashTable h = new HashTable(); h.Add(key, value); //insert hashtable into array ha[array_element_number] = h;
Now you pass yourha
array around, and get the hashtables back like this:HashTable h = ha[array_element_number];
If you defined your array in another way, likeobject[]
or so, you will have to do a cast when retrieving the hashtable. -
//Create array of hashtables HashTable[] ha = new HashTable[number_of_elements_in_array](); //create a hashtable and populate it HashTable h = new HashTable(); h.Add(key, value); //insert hashtable into array ha[array_element_number] = h;
Now you pass yourha
array around, and get the hashtables back like this:HashTable h = ha[array_element_number];
If you defined your array in another way, likeobject[]
or so, you will have to do a cast when retrieving the hashtable. -
I want to write a subroutine which fetches data from a database, save column to a hashtable (columname=columnvalue) and then put each dataset hashtable into an array ... then i want to return this array to the main program and run through each element and dereference the element back to a hashtable ... in perl i know how to do this - but have no clue how this had to be writen in c# ... (perl: %hash = %$array[element]) i trie something to put a hashtable into an array of string[] but i don't know how to get the hash back from the element of the string[] ... Thanks for your help! Matthias
Sounds like your trying to apply Perl design to a C# program. Before you go too far, you should first try to understand the database access classes in the System.Data namespace. A DataTable acts a lot like an array of DataRows, where individual fields can be accessed by name. But if you really want something like a perl array of hashes, it might look like this:
using System.Collections; ... ArrayList a = new ArrayList(); Hashtable h = new Hashtable(); h.Add( "One", 1 ); h.Add( "Two", 2 ); a.Add( h ); // Later h = (Hashtable)a[0]; Console.WriteLine( h["One"] );
Burt Harris