FAQ
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 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-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
http://learn.perl.org/

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 3 | next ›
Discussion Overview
groupbeginners @
categoriesperl
postedSep 23, '13 at 12:12p
activeSep 23, '13 at 1:22p
posts3
users2
websiteperl.org

2 users in discussion

Luca Ferrari: 2 posts Jing Yu: 1 post

People

Translate

site design / logo © 2022 Grokbase