Passing hash table by reference


  1.    12 Sep 2007 #1
    Orimslala Guest

    Passing hash table by reference


    Hi,

    Could someone show me the correct way to pass a hash table by
    reference to a function in powershell so i can access the data
    populated in the hash table outside the function. Within the funciton,
    i have my data populated in the hash table however when i leave the
    function, the count of the hash table returns 0 :-(

    ReadConfigFile $xmlfile ([REF]$hashTable)
    {
    switch( $xmlreader.NodeType )
    {
    switch( $xmlreader.Name )
    {
    "SomeRegex"
    {
    $PRIVATE:tmpTable = @{}
    $reader.ReadToFollowing("somestuff")
    do
    {
    $xmlreader.MoveToAttribute("name")
    $name = $xmlreader.value
    $xmlreader.MoveToAttribute("src")
    $src = $xmlreader.Value
    $tmpTable[$name] = $src
    }while( $xmlreader.ReadToNextSibling("somestuff") )
    $hashTable = $tmpTable
    write-host $hashTable.Value.count # all ok here
    break
    }
    break
    }
    }

    $PRIVATE:aHashTable
    ReadConfigFile $xmlfile ([REF]$aHashTable)

    I also tried to assign the values directly to the hash table without
    using a temp hash table and still no joy...

    Afolabi

      My System SpecsSystem Spec

  2.    12 Sep 2007 #2
    Roman Kuzmin Guest

    Re: Passing hash table by reference


    Objects are always passed by reference, so that you don't have to use [ref]
    for hashtables (and other *objects*)

    function Test($h)
    {
    $h['key1'] = 'value1'
    $h['key2'] = 'value2'
    }

    $hash = @{}
    $hash['key'] = 'value'
    Test $hash
    $hash

    output:

    Name Value
    ---- -----
    key2 value2
    key value
    key1 value1

    --
    Thanks,
    Roman Kuzmin
    PowerShellFar and FarNET: http://code.google.com/p/farnet/


      My System SpecsSystem Spec

  3.    12 Sep 2007 #3
    Orimslala Guest

    Re: Passing hash table by reference


    On 12 Sep, 10:54, "Roman Kuzmin" <z...@xxxxxx> wrote:

    > Objects are always passed by reference, so that you don't have to use [ref]
    > for hashtables (and other *objects*)
    >
    > function Test($h)
    > {
    > $h['key1'] = 'value1'
    > $h['key2'] = 'value2'
    >
    > }
    >
    > $hash = @{}
    > $hash['key'] = 'value'
    > Test $hash
    > $hash
    >
    > output:
    >
    > Name Value
    > ---- -----
    > key2 value2
    > key value
    > key1 value1
    >
    > --
    > Thanks,
    > Roman Kuzmin
    > PowerShellFar and FarNET:http://code.google.com/p/farnet/
    << Objects are always passed by reference
    Thanks Roman....

      My System SpecsSystem Spec

Passing hash table by reference

Similar Threads
Thread Forum
Re: Hash Table not being returned properly
I haven't tried your code... By messed up, do you mean where it shows "FALSE"? Marco
PowerShell
Hash Table not returning properly in Function
Hi, I have an issue where I am trying to return the contents of a hash table from a function into my script. I am using two functions: The main...
PowerShell
Sorting a Hash Table before outputting it to html
I have a script that reads a file that contains user log in and log out data. I have a PS script that reads this on the fly for the current...
PowerShell
How to create a hash table from an array
I'm looking for the simplest way to turn an array of custom objects into a hashtable with some particular property of the custom object being the...
PowerShell
Adding data to a hash table
I have imported a csv using import-csv $clcode = import-csv $csvlocation | select clcode $clcode results are a list of 6 digit numbers, for the...
PowerShell
Variable as hash table issue
I've got the "space invaders" PS script on the net as an example, and I saw we can declare variable as has table to group similar variable. ...
PowerShell
How do I read a XML file into a hash table?
Examples needed. Thanks!
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • Old server
    About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 04:07.
    .