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

In reply to Cheking for duplicates by blackadder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.