EDIT Sorry I forgot the most important part here. Each key can have more than one value. Apologies to those who already answered. print
and join
will be used later to print multiple values for $key
on a single line.
In the example code below, assuming that the value $keyvalue
is constantly changing, I am attempting to use a single line (or something similarly contained) to test and see if the current $keyvalue
already exists. If it does, then do nothing. If it does not, then push it. This line would reside within a while statement which is why it needs to be contained within a few lines.
Preserving order does not matter as long as there are no duplicate values.
my $key = "numbers";
my $keyvalue = 1;
my %hash = ($key => '1');
push (@{$hash{$key}}, $keyvalue) unless exists $hash{$key};
I am not getting any errors with use strict; use warnings;
, but at the same time this is not working. In the example above, I would expect that since the default value is 1
that the $keyvalue
would not be pushed as it is also 1
. Perhaps I have gotten myself all turned around...
Are there adjustments to get this to work or any alternatives that can be used instead to accomplish the same thing?