1

I have a need of inserting an array of arrays into an array.And this whole array is a value for a key in a hash.i meant hash should look like this:

"one"
[
  [
    1,
    2,
  [
    [
      3,
      4
    ],
    [
      5,
      6
    ]
  ]
]
]

where one is the key here and remaining part if the value of for that key in the hash. observe that the array of arrays [3,4] and [5,6] is the third element in the actual array. the first two elements are 1 and 2.

I have written a small program for doing the same.

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 1;
$Data::Dumper::Useqq = 1;
$Data::Dumper::Deparse = 1;

my %hsh;
my @a=[1,2];
my @b=[[3,4],[5,6]];
$hsh{"one"}=\@a;
push @{$hsh{"one"}},@b;
print Dumper(%hsh);

But this prints as below:

"one"
[
  [
    1,
    2
  ],   #here is where i see the problem.
  [
    [
      3,
      4
    ],
    [
      5,
      6
    ]
  ]
]

I can see that the array of arrays is not inserted into the array. could anybody help me with this?

1
  • [...] creates an array reference, not an array. So @a=[1,2] creates an array of one element. This may be a source of your problem, but I'm not sure: The intendation of your expected data structure was confusing. @a=(1,2) would create an array of two elements. Are you sure you dont want %hsh = ( one => [1, 2, [ 3, 4 ], [5, 6]] )? Commented Jun 6, 2013 at 6:08

2 Answers 2

1

First, a note: Only pass scalars to Dumper. If you want to dump an array or hash, pass a reference.

Then there's the question of what you expect. You say you expect

[ [ 1, 2, [ [ 3, 4 ], [5, 6] ] ] ]

But I think you really expect

[ 1, 2, [ [ 3, 4 ], [5, 6] ] ]

Both errors have the same cause.

[ ... ]

means

do { my @anon = ( ... ); \@anon }

so

my @a=[1,2];
my @b=[[3,4],[5,6]];

is assigning a single element to @a (a reference to an anonymous array) an a single element to @b (a reference to a different anonymous array).

You actually want

my @a=(1,2);
my @b=([3,4],[5,6]);

So from

my %hsh;
$hsh{"one"}=\@a;
push @{$hsh{"one"}},@b;
print(Dumper(\%hsh));

you get

{
  "one" => [
    1,
    2,
    [
      3,
      4
    ],
    [
      5,
      6
    ]
  ]
}
Sign up to request clarification or add additional context in comments.

Comments

0
use strict;
use warnings;
use Data::Dumper;
$Data::Dumper::Terse = 1;
$Data::Dumper::Indent = 1;
$Data::Dumper::Useqq = 1;
$Data::Dumper::Deparse = 1;

my %hsh;
my @a=(1,2); # this should be list not array ref
my @b=([3,4],[5,6]); # this should be list conatining array ref
push (@a, \@b); #pushing ref of @b
push (@{$hsh{'one'}}, \@a); #pushing ref of @a

print Dumper(%hsh);

Output:

"one"
[
  [
    1,
    2,
    [
      [
        3,
        4
      ],
      [
        5,
        6
      ]
    ]
  ]
]

Updated:

my %hsh;
my @a=( 1,2 );
my @b=( [3,4],[5,6] );
push (@a, @b); # removed ref of @b
push (@{$hsh{'one'}}, @a); #removed ref of @a

print Dumper(\%hsh);

Output:
{
  "one" => [
    1,
    2,
    [
      3,
      4
    ],
    [
      5,
      6
    ]
  ]
}

1 Comment

Are you sure that's it? You seem to have an an extra array. See my answer.

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.