Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Writing to a hashtable and storing to another hashtable

$
0
0

I am trying to break up an array into multiple arrays and use the data in the new arrays as values in a "temp" hashtable that will be added to a another hashtable.  unfortunately, the resulting hashtable has the same data in each stored hash (see results).   viewing the "temphash" (hash that is written to final hash) as it is produced shows it is getting properly created with different data each time.  Is the final autohash storing a reference to temphash?  what is the best way to do this or is there a glaring error in code?  thanks for any help.

$keys = ("f1","f2","f3","f4","f5","f6")
$values = (1,2,3,4,5,6,7,8,9,10,11,12)
$colcnt = $keys.count
$num = $values.count / $colcnt

$autohash = @{}
$temphash = @{}
$temparray = @()
for ($i=1; $i -le $num; $i++){

    $start = (($i-1) * $colcnt)
    $end = (($i) * $colcnt)
    $temparray = $values[$start..$end]
    for($j=0; $j -lt $keys.length; $j++){
        $temphash[$keys[$j]] = $temparray[$j]
    }
    $autohash.add($i,$temphash)
}

Name                           Value
----                           -----
f4                             10
f2                             8
f3                             9
f6                             12
f5                             11
f1                             7
****************************
f4                             10
f2                             8
f3                             9
f6                             12
f5                             11
f1                             7



Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>