locked
Writing to a hashtable and storing to another hashtable RRS feed

  • Question

  • 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


    • Edited by mlhii Wednesday, March 19, 2014 2:10 PM
    Wednesday, March 19, 2014 1:21 PM

Answers

  • as a couple of asides:

    - $temparray never needed initialization in the first place, as when it is set to $values[$start..$end] this value is not at all dependent on what $temparray might have previously been set to.

    - $temparray should be set to $values[$start..($end-1)], as otherwise it is set to (1,2,3,4,5,6,7) on the first pass instead of (1,2,3,4,5,6). Regardless, it is set to (7,8,9,10,11,12) on the last pass, because, even though the indices on $values goes only from 0 to 11, $values[6..12] is truncated to $values[6..11] because $values[12] returns nothing.

    as to why $temphash needed to be initialized in the loop, I agree with Rhys - don't ask. ;-)


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Marked as answer by mlhii Wednesday, March 19, 2014 3:14 PM
    Wednesday, March 19, 2014 2:44 PM
  • Reinitialize your temphash and temparray variables in the loop.  Probably don't need to for temparray, but I'd do it for aesthetics:

    $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 = @{}
    for ($i=1; $i -le $num; $i++){
        $temphash = @{}
        $temparray = @()
    
        $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)
    }

    Don't ask me why...

    • Proposed as answer by Mike Laughlin Wednesday, March 19, 2014 2:15 PM
    • Marked as answer by mlhii Wednesday, March 19, 2014 2:15 PM
    Wednesday, March 19, 2014 2:08 PM

All replies

  • Reinitialize your temphash and temparray variables in the loop.  Probably don't need to for temparray, but I'd do it for aesthetics:

    $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 = @{}
    for ($i=1; $i -le $num; $i++){
        $temphash = @{}
        $temparray = @()
    
        $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)
    }

    Don't ask me why...

    • Proposed as answer by Mike Laughlin Wednesday, March 19, 2014 2:15 PM
    • Marked as answer by mlhii Wednesday, March 19, 2014 2:15 PM
    Wednesday, March 19, 2014 2:08 PM
  • well that was simple.  thanks Rhys.  all is well.
    Wednesday, March 19, 2014 2:13 PM
  • as a couple of asides:

    - $temparray never needed initialization in the first place, as when it is set to $values[$start..$end] this value is not at all dependent on what $temparray might have previously been set to.

    - $temparray should be set to $values[$start..($end-1)], as otherwise it is set to (1,2,3,4,5,6,7) on the first pass instead of (1,2,3,4,5,6). Regardless, it is set to (7,8,9,10,11,12) on the last pass, because, even though the indices on $values goes only from 0 to 11, $values[6..12] is truncated to $values[6..11] because $values[12] returns nothing.

    as to why $temphash needed to be initialized in the loop, I agree with Rhys - don't ask. ;-)


    Al Dunbar -- remember to 'mark or propose as answer' or 'vote as helpful' as appropriate.

    • Marked as answer by mlhii Wednesday, March 19, 2014 3:14 PM
    Wednesday, March 19, 2014 2:44 PM
  • thanks al.  corrections made.
    Wednesday, March 19, 2014 3:15 PM