odd behavior - arrays to hash table

Hi

I'm trying to create a hash table from two arrays, for which I'm going to plot some charts. But dependen on how I do it I get some different and weird behavior.

These are the two variables:

PowerCLI> $xlsx.group[0].time.Split(",")
200815124551
200815125051
200815125551
200815010051
200815010551
200815011051
200815011551
200815012051
200815012551
200815013051
PowerCLI> $xlsx.group[0].metricvalue.Split(",")
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0

My script1:

function create_hash ([array] $keys, [array] $values) {
$h = @{}
$i = 0
do {$h[$keys[$i]] = $values[$i]; $i++;} until ($i -eq $keys.length)
$h
}

Output:

PowerCLI> create_hash $xlsx.group[0].time.Split(",") $xlsx.group[0].metricvalue.Split(",")

Name                           Value
----                           -----
200815010051                   0.0
200815125551                   0.0
200815011551                   0.0
200815011051                   0.0
200815012551                   0.0
200815010551                   0.0
200815013051                   0.0
200815012051                   0.0
200815124551                   0.0
200815125051                   0.0


PowerCLI> create_hash $xlsx.group[0].metricvalue.Split(",") $xlsx.group[0].time.Split(",")

Name                           Value
----                           -----
0.0                            200815013051

So looking a the two outputs the first one the time, comes out 'out of order' and the second one only one value comes out :(

Changing things a bit still doesn't do it.

My Script2:  

function create_hash ([array] $keys, [array] $values) {
$h = @{}
$i = 0
do {$h[$keys[$i]] = $values[$i]; $i++;$h} until ($i -eq $keys.length)
}

Output:

PowerCLI> create_hash $xlsx.group[0].time.Split(",") $xlsx.group[0].metricvalue.Split(",")


Name                           Value
----                           -----
200815124551                   0.0
200815125051                   0.0
200815124551                   0.0
200815124551                   0.0
200815125051                   0.0
200815125551                   0.0
200815124551                   0.0
200815010051                   0.0
200815125051                   0.0
200815125551                   0.0
200815124551                   0.0
200815010051                   0.0
200815125051                   0.0
200815125551                   0.0
200815010551                   0.0
200815010051                   0.0
200815125551                   0.0
200815011051                   0.0
200815010551                   0.0
200815124551                   0.0
200815125051                   0.0
200815010051                   0.0
200815125551                   0.0
200815011551                   0.0
200815011051                   0.0
200815010551                   0.0
200815124551                   0.0
200815125051                   0.0
200815010051                   0.0
200815125551                   0.0
200815011551                   0.0
200815011051                   0.0
200815010551                   0.0
200815012051                   0.0
200815124551                   0.0
200815125051                   0.0
200815010051                   0.0
200815125551                   0.0
200815011551                   0.0
200815011051                   0.0
200815012551                   0.0
200815010551                   0.0
200815012051                   0.0
200815124551                   0.0
200815125051                   0.0
200815010051                   0.0
200815125551                   0.0
200815011551                   0.0
200815011051                   0.0
200815012551                   0.0
200815010551                   0.0
200815013051                   0.0
200815012051                   0.0
200815124551                   0.0
200815125051                   0.0


PowerCLI> create_hash $xlsx.group[0].MetricValue.Split(",") $xlsx.group[0].time.Split(",")

Name                           Value
----                           -----
0.0                            200815124551
0.0                            200815125051
0.0                            200815125551
0.0                            200815010051
0.0                            200815010551
0.0                            200815011051
0.0                            200815011551
0.0                            200815012051
0.0                            200815012551
0.0                            200815013051

Again the first one is way off and the last one is correct but name and value should be switch around...

Can't get my head around this, what's wrong ?

August 25th, 2015 7:11am

You are overcomplicating this.  It also does not make much sense to use a hash,

$times=$xlsx.group[0].time.Split(',')
$values=$xlsx.group[0].metricvalue.Split(',')

$hash=@{}
for($i=0;$i -lt $times.Count;$i++){
     $hash.Add($times[$i],$values[$i])
}

The values in $times must be unique.

Free Windows Admin Tool Kit Click here and download it now
August 25th, 2015 9:46am

Thx... But data is still out of order... Why is it add in a random order ? The same problem I had above

PowerCLI> $hash

Name                           Value
----                           -----
200815010051                   0.0
200815125551                   0.0
200815011551                   0.0
200815011051                   0.0
200815012551                   0.0
200815010551                   0.0
200815013051                   0.0
200815012051                   0.0
200815124551                   0.0
200815125051                   0.0

August 26th, 2015 3:44am

There is no order for a hash table.  Why would you assume that there is one?

Data has no specific order without some ordering mechanism.  Order is a human thing.  It is not native to a computer.  You could use an ordered list or sort the data.  You could also use objects and supply an index.

Free Windows Admin Tool Kit Click here and download it now
August 26th, 2015 4:04am

Hi mryom,

a hash does not guarantee order of elements. It's great for accessing values by name without needing to create (and manage) variables each time.

Try building custom objects instead (stealing a bit from jrv's example up there):

$times=$xlsx.group[0].time.Split(',')
$values=$xlsx.group[0].metricvalue.Split(',')

$list=@()
for($i=0;$i -lt $times.Count;$i++){
    $list += New-Object PSObject -Property @{ Time = $times[$i]; Value = $values[$i] }
}
$list

Cheers,
Fred

August 26th, 2015 4:05am

Yes - object collections are more flexible but they still do not guarantee order.  The initial order comes with no guarantee.
Free Windows Admin Tool Kit Click here and download it now
August 26th, 2015 4:14am

Yes - object collections are more flexible but they still do not guarantee order.  The initial order comes with no
August 26th, 2015 4:31am

If you want a hash ordered by how the elements are added use this declaration:

$hash=[ordered]@{}

If you want a sorted list then use this:

$hash=[collections.sortedlist]@{}

You can also declare any method or ordering by declaring a custom collection with a custom comparison operator.

Free Windows Admin Tool Kit Click here and download it now
August 26th, 2015 4:34am

Yes - object collections are more flexible but they still do not guarantee order.  The initial order comes with no guaran

August 26th, 2015 4:43am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics