Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8768007
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:57:43+00:00 2026-06-13T16:57:43+00:00

I am working on a perl script which calls VMware vSphere perl API. I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:57:45+00:00Added an answer on June 13, 2026 at 4:57 pm
    map BLOCK LIST
    

    is very similar to

    do {
       my @list;
       for (LIST) {
          push @list, do BLOCK;
       }
       @list
    };
    

    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

    my $datastore_map = {
        map { $_->{'mo_ref'}->{'value'} => $_ }
         @{ $datastore_views || [ ] }
    };
    

    Before we start, let’s rewrite that to the following to make things a bit simpler.

    my %datastore_map =
        map { $_->{'mo_ref'}->{'value'} => $_ }
         @{ $datastore_views || [ ] };
    
    my $datastore_map = \%datastore_map;
    

    So that means what you have is equivalent to

    my %datastore_map = do {
       my @list;
       for (@{ $datastore_views || [ ] }) {
          push @list, do { $_->{'mo_ref'}->{'value'} => $_ };
       }
       @list
    };      
    
    my $datastore_map = \%datastore_map;
    

    And we can simplify that to

    my %datastore_map;
    for (@{ $datastore_views || [ ] }) {
       $datastore_map{ $_->{'mo_ref'}->{'value'} } = $_;
    }      
    
    my $datastore_map = \%datastore_map;
    

    or

    my %datastore_map;
    if ($datastore_views) {
       for (@$datastore_views) {
          $datastore_map{ $_->{'mo_ref'}->{'value'} } = $_;
       }
    }      
    
    my $datastore_map = \%datastore_map;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a Perl script which is called from a server side include
I have a small perl script ( found here ) which adds command line
I am working on a Perl script which needs to populate an XML file
I am working on a perl script which has to commit a new file
I'm working on a Perl script where the user adds a number of set
I'm working in a Perl script and I'd like to use named parameters to
I am working on a perforce-perl script that creates labels. Due to repeated execution
While writing script I noticed some very strange Perl behavior. The task is to
I am working on a log parsing script using Perl. I am reading the
I'm working on a large Perl application which gets bundled with PAR , along

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.