Exercise: Sum All Elements of a Nested Array

Work through this exercise to sum all elements of a nested array.

Problem statement

In this challenge, we’ll use references and nested data structures to add all the elements of a two-dimensional nested array. The sum must be stored in a variable $sum.

Sample input

my @nested_array = ([qw(1 2)], [qw(3 4 5)], [qw(5 10)])

Expected output

30

Coding challenge

This is a simple problem related to references and nested data structure concepts we have covered. The input matrix has already been created. You must use it in your code and find the sum of all its elements.

If you feel stuck, you can refer to the hints. For the solution, click the “Show Solution” button on the code playground. For further explanation of the solution, you can navigate to the next lesson.

Good luck!

Get hands-on with 1200+ tech skills courses.