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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:03:46+00:00 2026-06-18T02:03:46+00:00

How can I Initialise and clear multiple hash in one line. Ex: my %hash1

  • 0

How can I Initialise and clear multiple hash in one line.

Ex:

my %hash1 = ();
my %hash2 = ();
my %hash3 = ();
my %hash4 = ();

to

my ( %hash1, %hash2, %hash3, %hash4 ) = ?
  • 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-18T02:03:47+00:00Added an answer on June 18, 2026 at 2:03 am

    It appears (from your comments) that you really want to empty hashes that already have stuff in them. You can do it like this:

    (%hash1,%hash2,%hash3) = ();
    

    Complete example:

    use strict;
    use warnings;
    
    my %hash1 = ('foo' => 1);
    my %hash2 = ('bar' => 1);
    my %hash3 = ('baz' => 1);
    
    (%hash1,%hash2,%hash3) = ();
    
    print (%hash1,%hash2,%hash3);
    

    A variable declaration always gives you an empty variable, so there is no need to set it to empty. This is true even in a loop:

    for (0..100)
    {
        my $x;
        $x++;
        print $x;
    }
    

    This will print 1 over and over; even though you might expect $x to retain its value, it does not.

    Explanation: Perl allows list assignment like ($foo,$bar) = (1,2). If the list on the right is shorter, any remaining elements get assigned undef. Thus assigning the empty list to a list of variables makes them all undefined.

    Another useful way to set a bunch of things is the x operator:

    my ($x,$y,$z) = (100)x3;
    

    This sets all three variables to 100. It doesn’t work so well for hashes, though, because each one needs a list assigned to it.

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

Sidebar

Related Questions

I know that we can initialise an array in one of two ways: Loop
I would like to initialize several auto-vivifying hashes by one-line expression. So far I
My question in short: Can I use a single Factory for multiple Controllers? More
What is a clean way to declare multiple constructors for one class? For example,
I have a database with files which can be searched, browsed and have multiple
Basically the setup is almost the same as [[ One callback for multiple json
How can i add a line of text to a multi-line TextBox ? e.g.
How can I clean this so that the code handles multiple listings better? I
This is actually a much more concise, much more clear question than the one
I find it annoying that I can't clear a list. In this example: a

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.