blackadder has asked for the wisdom of the Perl Monks concerning the following question:
I have couple of data centres, each data centre may or may not have clusters in them, and each data centre will have a number of hosts.foreach (@$Datacenter_views) { my %seen; my $DC = $_->name; print "$DC, "; # get all clusters under this datacenter my $DC_view = Vim::find_entity_view(view_type => 'Datacenter', fil +ter => { name => $DC }); my $Cluster_views = Vim::find_entity_views(view_type => 'ClusterCo +mputeResource', begin_entity => $DC_view); foreach my $cluster (@$Cluster_views) { my $Cluster_name = $cluster->name; # printf "%s\n",$Cluster_name; # get all hosts in this cluster my $Cluster_view = Vim::find_entity_view(view_type => 'Cluster +ComputeResource', filter => { name => $cluster->name }); my $Hosts_view = Vim::find_entity_views(view_type => 'HostSyst +em', begin_entity => $Cluster_view); # print all hosts in the cluster foreach my $hosts (@$Hosts_view) { my $host_name = $hosts->name; printf"%s, %s\n", $Cluster_name, $hosts->name; $seen{$host_name} =1; } } # get all hosts under this datacenter my $hosts_views = Vim::find_entity_views(view_type => 'HostSystem' +, begin_entity => $DC_view); foreach my $hosts (@$hosts_views) { # This is the problem unless ( %seen) { printf "%s\n",$hosts->name; } } exit(); }
This command will get me all hosts in a given cluster namemy $Cluster_views = Vim::find_entity_views(view_type => 'ClusterCo +mputeResource', begin_entity => $DC_view);
This command will get a list all hosts in a given data centremy $Cluster_view = Vim::find_entity_view(view_type => 'Cluster +ComputeResource', filter => { name => $cluster->name }); my $Hosts_view = Vim::find_entity_views(view_type => 'HostSyst +em', begin_entity => $Cluster_view);
Now i want to print them out in this mannar:my $hosts_views = Vim::find_entity_views(view_type => 'HostSystem' +, begin_entity => $DC_view);
So to avoid duplication, I thought before I print the list of hosts in the data center, which will also include all hosts in clusters, I get them marked with $seen{$host_name}=1.Data Centre, Cluster, Host (only if the host is in a cluster) Data centre, Host (only if the host not in a data centre)
I check if a host name is marked then it won’t print is unless its unmarked. It makes perfect sense to me, but Perl having none of that! It prints the whole list of hosts in the data centre regardless of the fact that they have already been printed as part of a cluster contents.foreach my $hosts (@$hosts_views) { # This is the problem unless (! %seen) { printf "%s\n",$hosts->name; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Cheking for duplicates
by Bloodnok (Vicar) on Sep 17, 2009 at 12:23 UTC | |
by blackadder (Hermit) on Sep 17, 2009 at 12:54 UTC |