I have the following data structure:
@keys = [1, 2, 3, 4];
And using a loop (for) from 1 to 4, I want to create a new data structure like
$new = +{ key => '1',
meaning => '',
time => '', };
So, basically I would have in this case four $new data structures.
Do I need to use the map function?
TLP
68.2k10 gold badges97 silver badges156 bronze badges
2 Answers 2
Confusing question, but I think you problem is that
@keys = [1, 2, 3, 4];
is probable not what you mean. It should be either
@keys = (1, 2, 3, 4);
or
$keysref = [1, 2, 3, 4];
I'll assume the first. Then yes, you could populate an array of records with map
@records = map( {key => $_,meaning => '',time => ''}, @keys );
answered May 30, 2013 at 8:17
Sodved
8,6272 gold badges34 silver badges47 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
my @keys = (1, 2, 3, 4);
my @array = map +{
key => $_,
meaning => '',
time => '',
} @keys;
now @array has same number of elements as @keys and they are hash references.
answered May 30, 2013 at 8:16
mpapec
50.7k8 gold badges71 silver badges133 bronze badges
lang-perl
maporforeachalthoughmapis more common/idiomatic.@keys = [1, 2, 3, 4]does not assign an array, it assigns an array of arrays (well, the first element of the array is an array).