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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:21:17+00:00 2026-05-12T23:21:17+00:00

I would like to properly understand hashes in Perl. I’ve had to use Perl

  • 0

I would like to properly understand hashes in Perl. I’ve had to use Perl intermittently for quite some time and mostly whenever I need to do it, it’s mostly related to text processing.

And everytime, I have to deal with hashes, it gets messed up. I find the syntax very cryptic for hashes

A good explanation of hashes and hash references, their differences, when they are required etc. would be much appreciated.

  • 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-12T23:21:18+00:00Added an answer on May 12, 2026 at 11:21 pm

    A simple hash is close to an array. Their initializations even look similar. First the array:

    @last_name = (
      "Ward",   "Cleaver",
      "Fred",   "Flintstone",
      "Archie", "Bunker"
    );
    

    Now let’s represent the same information with a hash (aka associative array):

    %last_name = (
      "Ward",   "Cleaver",
      "Fred",   "Flintstone",
      "Archie", "Bunker"
    );
    

    Although they have the same name, the array @last_name and the hash %last_name are completely independent.

    With the array, if we want to know Archie’s last name, we have to perform a linear search:

    my $lname;
    for (my $i = 0; $i < @last_name; $i += 2) {
      $lname = $last_name[$i+1] if $last_name[$i] eq "Archie";
    }
    print "Archie $lname\n";
    

    With the hash, it’s much more direct syntactically:

    print "Archie $last_name{Archie}\n";
    

    Say we want to represent information with only slightly richer structure:

    • Cleaver (last name)
      • Ward (first name)
      • June (spouse’s first name)
    • Flintstone
      • Fred
      • Wilma
    • Bunker
      • Archie
      • Edith

    Before references came along, flat key-value hashes were about the best we could do, but references allow

    my %personal_info = (
        "Cleaver", {
            "FIRST",  "Ward",
            "SPOUSE", "June",
        },
        "Flintstone", {
            "FIRST",  "Fred",
            "SPOUSE", "Wilma",
        },
        "Bunker", {
            "FIRST",  "Archie",
            "SPOUSE", "Edith",
        },
    );
    

    Internally, the keys and values of %personal_info are all scalars, but the values are a special kind of scalar: hash references, created with {}. The references allow us to simulate “multi-dimensional” hashes. For example, we can get to Wilma via

    $personal_info{Flintstone}->{SPOUSE}
    

    Note that Perl allows us to omit arrows between subscripts, so the above is equivalent to

    $personal_info{Flintstone}{SPOUSE}
    

    That’s a lot of typing if you want to know more about Fred, so you might grab a reference as sort of a cursor:

    $fred = $personal_info{Flintstone};
    print "Fred's wife is $fred->{SPOUSE}\n";
    

    Because $fred in the snippet above is a hashref, the arrow is necessary. If you leave it out but wisely enabled use strict to help you catch these sorts of errors, the compiler will complain:

    Global symbol "%fred" requires explicit package name at ...
    

    Perl references are similar to pointers in C and C++, but they can never be null. Pointers in C and C++ require dereferencing and so do references in Perl.

    C and C++ function parameters have pass-by-value semantics: they’re just copies, so modifications don’t get back to the caller. If you want to see the changes, you have to pass a pointer. You can get this effect with references in Perl:

    sub add_barney {
        my($personal_info) = @_;
    
        $personal_info->{Rubble} = {
            FIRST  => "Barney",
            SPOUSE => "Betty",
        };
    }
    
    add_barney \%personal_info;
    

    Without the backslash, add_barney would have gotten a copy that’s thrown away as soon as the sub returns.

    Note also the use of the “fat comma” (=>) above. It autoquotes the string on its left and makes hash initializations less syntactically noisy.

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

Sidebar

Related Questions

Conceptually, I would like to accomplish the following but have had trouble understand how
Conceptually, I would like to accomplish the following but have had trouble understand how
I've never used TDD and unit testing properly, but would like to learn some
I would like to use the MFMailComposeViewController mailComposeDelegate property with completion block syntax, but
Its properly because I do not understand completely, but I have had these issues
What I would like to achieve is to be able to address some JSF
I would like some help in setting up a project in SVN with regards
I am using Rails 3.1.0 and I would like to understand why the following
I would like to debug and profile some 64-bit software performing unaligned accesses, like
I'm trying to understand how to properly use mod_rewrite. I have posts in 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.