#!/usr/bin/perl use warnings; use strict; my @str = ('{{1,1},2,{1,1}}', # the function should return 10 '{1,{4,{6}}}', # the function should return 27 '{{1,1,1,1},{1,1,{2,2}},{1,{2,{3,3}}}}', # Buk 56 '{1,{{13}}}' # mine.. two digit tests 40 ); my $w = 0; # weight or depth print "$_\t=>\t".( eval join ('', map{ /{/ ? ++$w && '(' : /\d/ ? $w.'*'.$_ : /,/ ? '+' : /}/ ? $w-- && ')' : $_ }($_=~/\d+|./g) ) )."\n" for @str; #OUTPUT {{1,1},2,{1,1}} => 10 {1,{4,{6}}} => 27 {{1,1,1,1},{1,1,{2,2}},{1,{2,{3,3}}}} => 56 {1,{{13}}} => 40