my %hash = ( "abc" => 4, "def" => 8, ); my $stddev = 0; my @cons = qw(abc def); for my $n ( @array ){ for my $con ( @cons ){ $stddev += ($n - $hash{$con}) ** 2; } }