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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:29:05+00:00 2026-06-01T20:29:05+00:00

I am having trouble understanding the hash references and changing the hash in place,

  • 0

I am having trouble understanding the hash references and changing the hash in place, instead of returning it. I want to write a sub routine which will return a value from hash and also modify the hash. I was facing some issues while coding for it. So, I wrote the following basic code to understand modifying the hash in place.

#!/usr/local/bin/perl
#Check hash and array references
#Author: Sidartha Karna
use warnings;
use strict;
use Data::Dumper;

sub checkHashRef{
   my ($hashRef, $arrVal) = @_;
   my %hashDeref = %{$hashRef};

   $hashDeref{'check'} = 2;           
   push(@{$arrVal}, 3);

   print "There:" ;
   print Dumper $hashRef;      
   print Dumper %hashDeref;           
   print Dumper $arrVal

}


my %hashVal = ('check', 1);
my @arrVal = (1, 2);

checkHashRef(\%hashVal, \@arrVal);

print "here\n";
print Dumper %hashVal;
print Dumper @arrVal;

The output observed is:



    There:$VAR1 = {
          'check' => 1
        };
    $VAR1 = 'check';
    $VAR2 = 2;
    $VAR1 = [
          1,
          2,
          3
        ];
    here
    $VAR1 = 'check';
    $VAR2 = 1;
    $VAR1 = 1;
    $VAR2 = 2;
    $VAR3 = 3;

From the output, I inferred that, changes to hashDeref are not modifying the data in the reference. Is my understanding correct? Is there a way to modify the hash variable in place instead of returning it.

  • 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-01T20:29:07+00:00Added an answer on June 1, 2026 at 8:29 pm

    This is making a (shallow) copy of %hashVal:

    my %hashDeref = %{$hashRef};
    

    The hash-ref $hashRef still points to %hashVal but %hashDeref doesn’t, it is just a copy. If you want to modify the passed hash-ref in-place, then work with the passed hash-ref:

    sub checkHashRef{
       my ($hashRef, $arrVal) = @_;
       $hashRef->{'check'} = 2;
       #...
    

    That will leave your changes in %hashVal. In the array case, you never make a copy, you just dereference it in-place:

    push(@{$arrVal}, 3);
    

    and the change to $arrVal shows up in @arrVal.

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

Sidebar

Related Questions

I'm having trouble understanding why my code will not work asynchronously. When running asynchronously
I'm having trouble understanding why I have to place the button triggering the getJSON
I am having trouble understanding this java code. I want the image to twinkle
I'm having trouble understanding the :header_converters and :converters in FasterCSV. Basically, all I want
I'm having trouble understanding the for/in statement in JavaScript. The book which I'm using
I am having trouble understanding a question. The question asks first to write a
Im having trouble understanding class relationships after being asked to research it further, would
Im having trouble understanding this : why Microsoft put Xattribute under Xobject ? they
I'm having trouble understanding why I would use a context.xml file to declare a
I am still having trouble understanding what interfaces are good for. I read 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.