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 717199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:23:10+00:00 2026-05-14T05:23:10+00:00

I am building a hash where the keys, associated with scalars, are not necessarily

  • 0

I am building a hash where the keys, associated with scalars, are not necessarily unique. The desired behavior to be that if the key is unique, the value is the scalar. If the key is not unique, I want the value to be an array reference of the scalars associated witht the key. Since the hash is built up iteratively, I don’t know if the key is unique ahead of time. Right now, I am doing something like this:

if(!defined($hash{$key})){
   $hash{$key} = $val;
}
elseif(ref($hash{$key}) ne 'ARRAY'){
   my @a;
   push(@a, $hash{$key});
   push(@, $val);
   $hash{$key} = \@a;
}
else{
   push(@{$hash{$key}}, $val);
}

Is there a simpler way to do this?

  • 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-14T05:23:11+00:00Added an answer on May 14, 2026 at 5:23 am

    rjh is right on the money.

    I have written way too much code that does exactly what you describe–the hash value is an array ref unless it isn’t. Reams and reams of conditional type checking. Then one day it hit me, “Why am I writing all this crap? Just use an array ref everywhere, dummy” I said to myself. Since that day bluebirds fly down from the trees to sing to me whenever I walk in the park.

    push @{$hash{$key}}, $val;
    

    That’s all you have to do. If the key does not exist, the array is autovivified.

    If you don’t like autoviv, and want to be explicit do:

    $hash{$key} = [] unless exists $hash{$key};
    push @{$hash{$key}}, $val;
    

    Even this ‘verbose’ approach is much shorter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a small app that calculate hash from any given file to multiple
I'm building a website that makes use of a flexslider, but I want to
I'm building a hash that has 100 items and I also have a random
building a site using PHP and MySQL that needs to store a lot of
Building a Django app on a VPS. I am not very experienced with setting
Building a website that has English & Japanese speaking users, with the Japanese users
Building an iPhone OS application that will allow users to anonymously post information to
I'm building a commenting mechanism into an application that allows a programmer/plugin author to
I'm currently building an invite model that sends out email invitations with a link.
I'm building a web app for Sign In / Registration etc, and do not

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.