1

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?

4
  • 1
    You can use map or foreach although map is more common/idiomatic. Commented May 30, 2013 at 8:14
  • 1
    @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). Commented May 30, 2013 at 8:17
  • @mpapec Rolled back your edit. You cannot fix code in the question unless the OP clearly says it was a typo. Commented May 30, 2013 at 9:16
  • yes im terribly sorry, i meant @keys = (1, 2, 3, 4); Commented May 30, 2013 at 11:26

2 Answers 2

5

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 );
Sign up to request clarification or add additional context in comments.

Comments

1
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.

3 Comments

Your edit on the main question is questionable. If the OP really has that in his own code, then it will be impossible for anyone to help him.
my guess was that OP did typo.
Yes, why else would you have done it. Still, it is possible it is not a typo.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.