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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:35:39+00:00 2026-05-29T07:35:39+00:00

Just wondering if I am doing something wrong or if this is a perl

  • 0

Just wondering if I am doing something wrong or if this is a perl bug…I want to create an array of hash values. I am using ‘push’ to put the values onto the array. The first write of a hash to the array works fine, but when I push a second different hash onto the array, the first array element seems to get overwritten with what I just pushed onto the array. Why is this happening? See code below:

use Data::Dumper;

my %val;

%val = (key1 => "Val1");

my @myArr;

my $cnt = push(@myArr,\%val);

print "After push (should contain 1 element): " . Dumper(@myArr) . "\n";

%val = (key2 => "Val2");

my $cnt = push(@myArr,\%val);

print "After push 2: (should contain 2 different elements):" . Dumper(@myArr) . "\n";
print " You can see above that element 1 and 2 of the array equal each other when they should be different\n";
  • 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-29T07:35:41+00:00Added an answer on May 29, 2026 at 7:35 am

    “perl bug” – yeah, fat chance. 🙂

    You’re pushing a reference to a hash into your array, then changing that very hash, and then you are pushing the same reference once again.

    You probably need a copy or a whole different hash:

    Different variable:

    #!/usr/bin/perl
    
    use strict; # always use strict
    use warnings;
    use Data::Dumper;
    
    my ( %val, %other_val, @myArr );
    
    %val       = ( key1 => "Val1" );
    %other_val = ( key2 => "Val2" );
    
    push(@myArr, \%val);
    push(@myArr, \%other_val);
    
    print Dumper(\@myArr) . "\n";
    

    Copying:

    #!/usr/bin/perl
    
    use strict; # always use strict
    use warnings;
    use Data::Dumper;
    
    my ( %val, %other_val, @myArr );
    
    %val = ( key1 => "Val1" );
    push(@myArr, { %val } );
    
    %val = ( key2 => "Val2" );
    push(@myArr, { %val } );
    
    
    print Dumper(\@myArr) . "\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just wondering if there's a better way of doing this in SQL Server
Am I doing something wrong is this a known issue with the ASP.NET MVC
linux gcc 4.4.1 C99 I am just wondering is there any advantage using the
I'm using John Millikin's enumerator package and am trying to create something roughly equivalent
Just wondering if anyone could tell me of a simple way to create files
I've found a curious quirk of the MySqlDataReader.GetBytes implementation and just wondering if this
just wondering if anyone knows of a truly restful Put/delete implementation asp.net mvc preview
Just wondering if a .NET app can be compiled down to native machine code
Just wondering if there is an easy way to add the functionality to duplicate
Just wondering why people like case sensitivity in a programming language? I'm not trying

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.