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:08am

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:43am

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:36am

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

Other recent topics Other recent topics