Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Cheking for duplicates

by blackadder (Hermit)
on Sep 17, 2009 at 11:44 UTC ( [id://795861]=perlquestion: print w/replies, xml ) Need Help??

blackadder has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I have this script
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(); }
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.

This command will get me a list of all clusters in a given data centre
my $Cluster_views = Vim::find_entity_views(view_type => 'ClusterCo +mputeResource', begin_entity => $DC_view);
This command will get me all hosts in a given cluster name
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);
This command will get a list all hosts in a given data centre
my $hosts_views = Vim::find_entity_views(view_type => 'HostSystem' +, begin_entity => $DC_view);
Now i want to print them out in this mannar:

Data Centre, Cluster, Host (only if the host is in a cluster) Data centre, Host (only if the host not in a data centre)
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.

then with this bit of code;
foreach my $hosts (@$hosts_views) { # This is the problem unless (! %seen) { printf "%s\n",$hosts->name; } } }
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.

Can an enlightened one please help us out and show where I am going wrong?

Thanks
Blackadder

Replies are listed 'Best First'.
Re: Cheking for duplicates
by Bloodnok (Vicar) on Sep 17, 2009 at 12:23 UTC
    Your problem would appear to be that you're testing the hash for being empty, c/w a test for the existence of an individual key on the hash i.e. change unless (! %seen) to unless (! %seen->{$hosts->name}), or some such...

    A user level that continues to overstate my experience :-))
      Many Thanks indeed...It worked but like this
      foreach my $hosts (@$hosts_views) { unless ( $seen{$hosts->name}) { printf "%s\n",$hosts->name; } }
      Blackadder

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://795861]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-25 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found