Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Array of Hashes

Array of Hashes

Scheduled Pinned Locked Moved C#
csharpperldatabasedata-structurescryptography
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JJF007
    wrote on last edited by
    #1

    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

    C A B 3 Replies Last reply
    0
    • J JJF007

      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

      C Offline
      C Offline
      CBoland
      wrote on last edited by
      #2

      This may seem obvious, but have you tried System.Collections.Hastable? If you are working with only strings, the System.Collections.Specialized.NameValueCollection may offer better performance..

      1 Reply Last reply
      0
      • J JJF007

        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

        A Offline
        A Offline
        Arjan Einbu
        wrote on last edited by
        #3

        //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 your ha array around, and get the hashtables back like this: HashTable h = ha[array_element_number]; If you defined your array in another way, like object[] or so, you will have to do a cast when retrieving the hashtable.

        J 1 Reply Last reply
        0
        • A Arjan Einbu

          //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 your ha array around, and get the hashtables back like this: HashTable h = ha[array_element_number]; If you defined your array in another way, like object[] or so, you will have to do a cast when retrieving the hashtable.

          J Offline
          J Offline
          JJF007
          wrote on last edited by
          #4

          Thanks Arjan - works fine!

          1 Reply Last reply
          0
          • J JJF007

            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

            B Offline
            B Offline
            Burt Harris
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups