in reply to Assign a hash to an array

You probably want to push a hash reference to the array, not an array - it pushes a list of alternating keys and values. In order to make it work, you should also use a different reference for each iteration:

my %hash; $hash{term}{$key} = $value; push @return_array, \%hash;

or, more shortly

push @return_array, { term => { $key => $value} };

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Assign a hash to an array
by ravi45722 (Pilgrim) on Jun 27, 2016 at 09:44 UTC

    The second one is working very fine. Tq. But when I uses the first one its overwriting

    Output for push @return_array, \%hash;

    $VAR1 = [ { 'term' => { 'column09' => 'Something' } }, $VAR1->[0], $VAR1->[0] ]; $VAR1 = '{"must":[{"term":{"column09":"Something"}},{"term":{"column09 +":"Something"}},{"term":{"column09":"Something"}}]}';

    Output for push @return_array, { term => { $key => $value} };

    $VAR1 = [ { 'term' => { 'column08' => 'Submit' } }, { 'term' => { 'column10' => 'Delivered' } }, { 'term' => { 'column09' => 'Something' } } ]; $VAR1 = '{"must":[{"term":{"column08":"Submit"}},{"term":{"column10":" +Delivered"}},{"term":{"column09":"Something"}}]}';

      Understanding the unexpected result you're getting will really help you in the future.

      use strict; my (%hash,@array); $hash{A}='B'; push @array,\%hash;

      I have pushed into my array a hash reference. If I run this in the debugger, I can see the following:

      x @array 0 HASH(0x300331c4) 'A' => 'B'

      Resuming:

      $hash{B}='C'; push @array, \%hash;

      But:

      x @array 0 HASH(0x300331c4) 'A' => 'C' 1 HASH(0x300331c4) -> REUSED_ADDRESS

      I thought I pushed a different value into the array. I expect two hashes in my array, one with value B and one with C. But that's not what I get. A reference is not a copy of the variable, but a link to that variable. My array contains two references to the same variable, %hash. The first array element reflects the new value in %hash, 'C', and the second just says 'ditto'.

      use strict; my (%hash,@array); $hash{A}='B'; push @array, \%hash; my (%hash); $hash{A}='C'; push @array, \%hash;

      Debugger shows:

      x @array 0 HASH(0x30033194) 'A' => 'B' 1 HASH(0x3015f4e0) 'A' => 'C'

      This time, I created a new hash variable, so the array contains two distinct references. The first array element is unchanged by my actions.

      In this example, it appears I have lost that first %hash by reusing the name. In fact, I can no longer retrieve its values using that name. But the value is preserved by the reference in the array.

      Minor update made for clarity.

      But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)

      Are you sure you included the my %hash line, too?

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,