I have a simple hash defined somewhere in the main file
our %translations = (
"phrase 1" => "translation 1",
# ... and so on
);
In another file I want to add some more translations. That is, I want to do something like this:
push our %translations, (
"phrase N" => "blah-blah",
# ....
"phrase M" => "something",
);
Of course this code wouldn’t work: push doesn’t work with hashes. So my question is: what is a simple and elegant way to insert a hash of values into an existing hash?
I wouldn’t want to resort to
$translations{"phrase N"} = "blah-blah";
# ....
$translations{"phrase M"} = "something";
since in Perl you’re supposed to be able to do things without too much repetition in your code…
You can assign to a hash slice using the keys and values functions. As long as the hash isn’t modified between the calls,
keyswill return the keys in the same order thatvaluesreturns the values.