%hash = ( a => (($b =~ /(?\d+)/) ? $+{tag} : 0), b => (($b =~ /(?test)/) ? $+{tag} : 0), ); print "$_=#$hash{$_}#\n" for keys %hash;