array into hash array with qw

S

Slickuser

my @ar = ('x','1','y','z');
my %hashVar =
(
'key1' => [qw(1 2 3 4 5 5 6 7 8 9)],
'key2' => [qw(a b c d e) ]
);

@ar values are being populate my parsing a file and get push in.

How do I add @ar into 'key2'?
so it will contain a b c d e x 1 y z

Thanks.
 
M

Martijn Lievaart

my @ar = ('x','1','y','z');
my %hashVar =
(
'key1' => [qw(1 2 3 4 5 5 6 7 8 9)], 'key2' => [qw(a b c d e) ]
);

@ar values are being populate my parsing a file and get push in.

How do I add @ar into 'key2'?
so it will contain a b c d e x 1 y z

push @{$hashVar{key2}}, @ar;

Or more clearly:

my $arrayref = $hashVar{key2};
push @$arrayref, @ar;

HTH,
M4
 
J

Jens Thoms Toerring

Slickuser said:
my @ar = ('x','1','y','z');
my %hashVar =
(
'key1' => [qw(1 2 3 4 5 5 6 7 8 9)],
'key2' => [qw(a b c d e) ]
);
@ar values are being populate my parsing a file and get push in.
How do I add @ar into 'key2'?
so it will contain a b c d e x 1 y z

Others have pointed out how to stuff it into the array pointed
to by the 'key2' element when it already exists, but you can do
it also when creating the hash, using

my %hashVar =
(
key1 => [ qw( 1 2 3 4 5 5 6 7 8 9 ) ],
key2 => [ qw( a b c d e ), @ar ]
);
Regards, Jens
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top