• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Passing hash table by reference

O

Orimslala

#1
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 Computer

R

Roman Kuzmin

#2
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 Computer

O

Orimslala

#3
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 Computer

Users Who Are Viewing This Thread (Users: 1, Guests: 0)