0

I am looking for a solution in how to implement a hash array (with keys & values), and insert it (push it) into another hash array, in an uninstantiated element; for example:

$variable1 = {

          0 => {
                          'Mathematics' => 82,
                          'Art' => 99,
                          'Literature' => 88
                        },
          1 => {
                         'Mathematics' => 97,
                         'Literature' => 67
                       }
        };


$variable2 = { 'Biology' => 47, 'Theology' => 87 };

...

Where the first variable1 index is iterated chronologically as a counter 0, 1, 2, 3 ... n

Therefore final variable1 should be...

$variable1 = {

          0 => {
                          'Mathematics' => 82,
                          'Art' => 99,
                          'Literature' => 88
                        },
          1 => {
                         'Mathematics' => 97,
                         'Literature' => 67
                       }

          2 => {
                         'Biology' => 47, 
                         'Theology' => 87

                       } 


        };

2 Answers 2

2

Why are you using a hash as the outer structure? If it were an array, it would just be:

$variable1 = [
    {
        'Mathematics' => 82,
        'Art' => 99,
        'Literature' => 88
    },
    {
        'Mathematics' => 97,
        'Literature' => 67
    }
];

$variable2 = { 'Biology' => 47, 'Theology' => 87 };

push @$variable1, $variable2;

Or if you want to push a copy (so that changes to $variable1->[2] don't affect $variable2),

push @$variable1, { %$variable2 };

With the structure you have, you'd have to do something like:

# assuming numbers are always sequential and start at 0
$variable1->{ keys %$variable1 } = $variable2;

# or if not
my $max_index = List::Util::max( keys %$variable1 ) // -1;
$variable1->{ $max_index+1 } = $variable2;
Sign up to request clarification or add additional context in comments.

Comments

1

Since your keys are numeric, and you want to push new elements into structure, array of hashes is more natural to hold your data,

use strict;
use warnings;
use Data::Dumper;

my @variable1 = (
  {
    'Mathematics' => 82,
    'Art' => 99,
    'Literature' => 88
  },
  {
    'Mathematics' => 97,
    'Literature' => 67
  }
);


my $variable2 = { 'Biology' => 47, 'Theology' => 87 };

push @variable1, $variable2;

print Dumper \@variable1;

output

$VAR1 = [
      {
        'Art' => 99,
        'Literature' => 88,
        'Mathematics' => 82
      },
      {
        'Literature' => 67,
        'Mathematics' => 97
      },
      {
        'Biology' => 47,
        'Theology' => 87
      }
    ];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.