I am working on a perl script which calls VMware vSphere perl API. I was facing few problems and while googling, I found a solution which seems to work for me. However, one of the important piece of the code is written with map. Credit goes to the VMware community member who wrote it. Here is the code:
$datastore_map = map { $_->{'mo_ref'}->{'value'} => $_ } @{$datastore_views || [ ]};
I am not able to understand what above line is doing.
So, Could you help me to convert above line into a foreach loop or something similar. Believing in TMTOWTDI, there has to be another way to write same logic.
I tried to learn the map function a lot of times but it always bounces off my head. Hence I want to implement above logic without using the map function.
EDIT:
After reading the answers, I think the it should be %datastore_map and not $datastore_map. Because the next line of the code is $datastore = $datastore_map->{$moref->{'value'}};
It has to be a typo.
I will accept the answer when stacoverflow allows me to. Thanks for the quick repsonces. I spend almost an hour trying to understand it and failed at it.
Thanks.
EDIT:
Thanks @nickisfat, @choroba and @ikegami for the responses. All the answers are correct but I can accept only one. So I will go with the one which was posted first in the chronological order. Thanks again for the answers.
is very similar to
The code you posted makes no sense. Either the LHS of the assignment was actually a hash, or there’s a set of curlies missing. Let’s say your code is actually
Before we start, let’s rewrite that to the following to make things a bit simpler.
So that means what you have is equivalent to
And we can simplify that to
or