Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Passing hash table by reference

Reply
 
Old 09-12-2007   #1 (permalink)
Orimslala


 
 

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
Old 09-12-2007   #2 (permalink)
Roman Kuzmin


 
 

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
Old 09-12-2007   #3 (permalink)
Orimslala


 
 

Re: Passing hash table by reference

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

> 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
Reply

Thread Tools


Similar Threads
Thread Forum
Sorting a Hash Table before outputting it to html PowerShell
How to create a hash table from an array PowerShell
Adding data to a hash table PowerShell
Variable as hash table issue PowerShell
How do I read a XML file into a hash table? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46