#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Data::Walk; for my $arr ([ [1, 1], 2, [1, 1] ], # 10 [ 1, [4, [6]]], # 27 ) { my $sum = 0; walk sub { ref $_ or $sum += $_ * ($Data::Walk::depth - 1) }, $arr; say $sum; }