# count length of the strings my %len; foreach $nr (keys %data) { foreach my $att (@attr) { $len{$att} = 0; # <====== here update_max(%len, $att, $data{$nr}{$att}); } }