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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:55:41+00:00 2026-05-24T11:55:41+00:00

Below is a generalization of a block of code that I keep having to

  • 0

Below is a generalization of a block of code that I keep having to write when i want to build up a hash of arrays inside some sort of loop.

#get value and key that I want to use
my $value = getvalue();
my $key = getKey();

#add value to hash using key
if($hash_of_arrays{$key}){
    push(@{$hash_of_arrays{$key}}, $value);
}
else{
    $hash_of_arrays{$key} = [$value];
}

The if statement is very tedious to write for such a simple task but it needs to be done because pushing a value when its key is undefined in the hash causes problems. I’m just wondering if there is any shorthand to writing this – one where I dont have to write out $hash_of_arrays{$key} three times.

  • 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-24T11:55:42+00:00Added an answer on May 24, 2026 at 11:55 am

    push @{ $HoA{$key} }, $value; works perfectly well and is the recommended thing to do. If your code that uses it has “problems”, then ask about those problems instead.

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

Sidebar

Related Questions

Below is some code that I copied and edited. If I make the ColorsActivity
Below is the code that I am writing in HTML and I'm getting what
Below is the plugin code that i have tried to reuse from an old
Below I have a Json function and some php code which it retrieves. JavaScript
Below is my code that lets me to move the camera forward it works
Below is code that sits in an iframe. I'm able to get the variable
Below is my code that works fine on IE 8 but it doesn't work
Below is a heavily cut down version of some code I have public class
Below is the code of my adapter that allows to create a popup with
Below, I have written simple code that re-creates problems that have emerged as my

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.