0

I am trying to push values into hash. I want to add the values under 'par3'. e.g.

$VAR1 = { 'obj1' => ['par1', 
                     'par2', 
                     'par3' => ['par4','par5','par6',....]]}

I should also be able to add elements into 'par3' in case 'obj1'-'par1'-'par2'-'par3' matches. So far I have this, but I can't figure out how can I add "the second level" under 'par3':

push @{$a{$obj}},$par1,$par2,$par3
3
  • ['par1', 'par2', 'par3' => [ 'par4', 'par5', 'par6' ] ] is just a weird way of writing ['par1', 'par2', 'par3', [ 'par4', 'par5', 'par6' ] ] (a reference to an array four elements). Is that really what you want? Commented Jul 21, 2022 at 14:13
  • Every time I look at your question, I come up with a new question. It's extremely unclear what you want. The code shows that you are pushing par1, par2 and par3. But the dump shows these are the first values. So why is a push being used? , Are you asking for $VAR1->{ obj1 } = [ 'par1', 'par2', 'par3', [ 'par4', 'par5', 'par6' ] ];? If not, please explain why this would not do. Commented Jul 21, 2022 at 14:24
  • e.g. What's does the structure look before the operation you want to perform, and what do you want it to look like after. Please clarify your answer by editing it, not by adding comments (though feel free to leave a comment that contains @ikegami to let me know you've made these changes). Commented Jul 21, 2022 at 14:40

2 Answers 2

1

[ ... ] is a reference to an array. Array elements are scalars. So it is not possible to directly have the structure you seem to be requesting (ie. the par3 => [ ... ] pseudocode from your question). See perldsc

It's not obvious what you are trying to do but a couple of possible ideas might be to use a reference to a hash, or to replace the array with a hash:

use Data::Dumper;

$Var2a = {
    'obj1' => [
        'par1', 
        'par2', 
        { 'par3' => undef, }
    ],
};

push @{ $Var2a->{obj1}[2]{par3} }, 'par4', 'par5', 'par6';
print Dumper $Var2a;

$Var2b = {
    'obj1' => {
        'par1' => undef, 
        'par2' => undef, 
        'par3' => undef,
    },
};

push @{ $Var2b->{obj1}{par3} }, 'par4', 'par5', 'par6';
print Dumper $Var2b;
Sign up to request clarification or add additional context in comments.

1 Comment

I actually want to have in the key 'obj1' single values 'par1','par2','par3' and an array (list of elements) which contains {'par4','par5','par6',...}
0

I think you want to add children to par3. As such, I think you want pars to have children. If so, you need a different data structure.

# Ordered
my $obj1 = [
   { name => 'par1', children => [ ] },
   { name => 'par2', children => [ ] },
   { name => 'par3', children => [
      { name => 'par4', children => [ ] },
      { name => 'par5', children => [ ] },
      { name => 'par6', children => [ ] },
   ] },
];

or

# Unordered
my $obj1 = {
   par1 => { },
   par2 => { },
   par3 => {
      par4 => { },
      par5 => { },
      par6 => { },
   },
};

To append par7 to par3's children, you would use

# Ordered

use List::Util qw( first );

my $par7 = { name => 'par7', children => [ ] };

my $par3 = first { $_->{ name } eq 'par3' } @$obj1
   or die( "par3 not found" );

push @{ $par3->{ children } }, $par7;

or

# Unordered

$obj1->{ par3 }{ par7 } = { };

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.