if ( $output =~ /^(pattern1|pattern2)$/ ){ $hash{$key} = $1; } elsif ( $output_2 =~ /(pattern3)/ ){ $hash{$key} = $1; }