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

  • Home
  • SEARCH
  • 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 7080683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:49:31+00:00 2026-05-28T06:49:31+00:00

Task: to build hash using map, where keys are the elements of the given

  • 0

Task: to build hash using map, where keys are the elements of the given array @a, and values are the first elements of the list returned by some function f($element_of_a):

my @a = (1, 2, 3);
my %h = map {$_ => (f($_))[0]} @a;

All the okay until f() returns an empty list (that’s absolutely correct for f(), and in that case I’d like to assign undef). The error could be reproduced with the following code:

my %h = map {$_ => ()[0]} @a;

the error itself sounds like “Odd number of elements in hash assignment”. When I rewrite the code such that:

my @a = (1, 2, 3);
my $s = ()[0];
my %h = map {$_ => $s} @a;

or

my @a = (1, 2, 3);
my %h = map {$_ => undef} @a;

Perl does not complain at all.

So how should I resolve this — get first elements of list returned by f(), when the returned list is empty?

Perl version is 5.12.3

Thanks.

  • 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-05-28T06:49:32+00:00Added an answer on May 28, 2026 at 6:49 am

    I’ve just played around a bit, and it seems that ()[0], in list context, is interpreted as an empty list rather than as an undef scalar. For example, this:

    my @arr = ()[0];
    my $size = @arr;
    print "$size\n";
    

    prints 0. So $_ => ()[0] is roughly equivalent to just $_.

    To fix it, you can use the scalar function to force scalar context:

    my %h = map {$_ => scalar((f($_))[0])} @a;
    

    or you can append an explicit undef to the end of the list:

    my %h = map {$_ => (f($_), undef)[0]} @a;
    

    or you can wrap your function’s return value in a true array (rather than just a flat list):

    my %h = map {$_ => [f($_)]->[0]} @a;
    

    (I like that last option best, personally.)


    The special behavior of a slice of an empty list is documented under “Slices” in perldata:

    A slice of an empty list is still an empty list. […] This makes it easy to write loops that terminate when a null list is returned:

    while ( ($home, $user) = (getpwent)[7,0]) {
        printf "%-8s %s\n", $user, $home;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write an MSBuild task to build a database using FluentNhibernate mappings.
When I set out this morning the task seemed simple: build a list of
I'm using nant to build our product and have written a custom task to
Are the keywords used for project, target, task, function, ... in NAnt build files
I have the following question: I was given the task - to build an
I am using a pre-build task in Visual Studio 2008 that invokes msbuild: C:\WINDOWS\Microsoft.NET\Framework\v3.5\MSBuild.exe
I need my custom team build task to write some output to the build
I want to use the <nant> task in my build script but I want
I made a custom task in my TFS build to examine my project's GlobalAssemblyInfo.cs
I'm trying to call an Antlr task in my Ant build.xml as follows: <path

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.