Hi Luca,

Doesn't it autovivify $hash_ref->{$key} when you push $new_value to it?

At least when I tested the following code, it worked.

push @{ $hash_ref->{$key} }, $new_value;

Regards,
Jing
On 23 Sep 2013, at 20:12, Luca Ferrari <fluca1...@infinito.it> wrote:

> Hi,
> in my applications often I end up with an hash ref that contains array
> refs, and each time I have to insert a new element in the array for a
> specific key I write something like:
> 
> 
> if ( ! exists $hash_ref->{ $key } ){
>    $hash_ref->{ $key } = [];
> }
> 
> push @{ $hash_ref->{$key} }, $new_value;
> 
> 
> Is there a better and shorter pattern to push the new value in an
> array ref creating it if it does not yet exists in the hash?
> 
> Thanks,
> Luca
> 
> -- 
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
> 
> 

Reply via email to