How do I push a value onto a Perl hash of arrays?

%TEST ; 

... 
for  {
   sub atest
   }
 sub atest {
 ...
    push $TEST { TEST1 }[0] = "some value " 
}

How do I push values into a hash of arrays without knowing anything about index?

How do I achieve this?

Answers


This will add value to the end of array stored in hash by "TEST1" key.

push( @{ $TEST { TEST1 } }, "some value "); 

I've used @{...} to dereference array reference. Perl creates inner array reference automatically then needed.


The push function takes an array, so you must deference it back into an array:

push @{$TEST{TEST1}}, "some value";

Also, your style makes me think you are not using the strict pragma. A better way to write that code is:

#!/usr/bin/perl

use strict;
use warnings;

sub atest {
    my $test = shift;
    push @{$test->{TEST1}}, "some value";
}

my %test;
atest(\%test);

use Data::Dumper;

print Dumper \%test;

I think you want:

%TEST;
$TEST{TEST1}[0] = "some value"
push @{ $TEST{TEST1} }, "some other value"

Now, $TEST{TEST1} should be equivalent to ["some value", "some other value"].


Need Your Help

Is there any useful Android Animation Tutorial for beginners?

android animation transition

Is there any good android animation tutorials to do in views or the transitions? A complete tutorial or guide may help alot.

Bind input value to ng-click button (send value as parameter)

javascript html angularjs angularjs-ng-click angularjs-ng-model

I'm trying to bind the value from an input field to the parameter of my method on ng-click. Here's what I got, but it doesn't work, and I am not too sure if it's possible to do it this way?: