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

Forum statistics

Threads
474,416
Messages
2,571,562
Members
48,797
Latest member
shadowoftheunknown

Latest Threads

Top