VMware {code} Community
MR-Z
VMware Employee
VMware Employee

Get cloud.uuid optionvalue from extra config without iterating through the array?

I need to get the cloud.uuid (or any other optionValue for that matter) from the extraConfig data object.  Is there a way to get to the value of a specific key without having to iterating through the array? As this is not a hash, attempt at getting to the value with this fails miserably:

$extra->{'could.uuid'}...

Here is an iteration approach that works.


my $extra = $vm->config->extraConfig;

        foreach (@$extra) {

                print $_->value if ($_->key =~ /cloud.uuid/);

        }

Thanks!

0 Kudos
4 Replies
stumpr
Virtuoso
Virtuoso

Unfortunately it's an array, so you'll have to iterate.  If you wanted to, you could convert it to a hash.

You could use 'grep' as well to get a one liner.  It won't be such a large array in any event that there is any real dramatic speed increase between a hash and an array.

Reuben Stump | http://www.virtuin.com | @ReubenStump
MR-Z
VMware Employee
VMware Employee

Thanks stumpr. Understood. Just wanted to see if anything magic out there...

0 Kudos
litty123
Contributor
Contributor

You can use config.extraConfig["cloud.uuid"] as the property name in PropertySpec pathset and that will retrieve only the cloud.uuid extraconfig.

0 Kudos
probo
Enthusiast
Enthusiast

Within perl you can do the following to find an element in an array:

if (grep {$_ eq $element} @TheArray) { print "Element '$element' found!\n" ; }

(Original article here How to find out if X is an element in an array? )

You can adapt this to your scenario by doing:

print $_->value if (grep {$_->key =~ /cloud.uuid/} @$extra);

0 Kudos