选择元素的最高数量,除非...... [英] Selecting highest count of element except when...
问题描述
所以我一直在研究这个perl脚本,它将分析和计算不同行空间中的相同字母。我已经将计数实现为哈希,但是在从该哈希的输出结果中排除 - 字符时遇到了麻烦。我尝试使用删除命令或下一步,但是我没有摆脱 - 输出中的计数。
So i have been working on this perl script that will analyze and count the same letters in different line spaces. I have implemented the count to a hash but am having trouble excluding a " - " character from the output results of this hash. I tried using delete command or next if, but am not getting rid of the - count in the output.
所以有了这个输入:
So with this input:
@extract = ------------------------------------------------------------------MGG-------------------------------------------------------------------------------------
以下代码:
And following code:
#Count selected amino acids.
my %counter = ();
foreach my $extract(@extract) {
#next if $_ =~ /\-/; #This line code does not function correctly.
$counter{$_}++;
}
sub largest_value_mem (\%) {
my $counter = shift;
my ($key, @keys) = keys %$counter;
my ($big, @vals) = values %$counter;
for (0 .. $#keys) {
if ($vals[$_] > $big) {
$big = $vals[$_];
$key = $keys[$_];
}
}
$key
}
我认为最常见的元素是G,与输出相同。如果元素中存在联系,比如说G = M,那么如果有一种方法可以同时显示这两个元素,那将是很好但不是必需的。任何关于如何删除或删除' - '的提示都非常感谢。我正在慢慢学习perl语言。
I expect the most common element to be G, same as the output. If there is a tie in the elements, say G = M, if there is a way to display both in that would be great but not necessary. Any tips on how to delete or remove the '-' is much appreciated. I am slowly learning perl language.
请让我知道我所要求的内容不清楚,或者需要更多信息,再次感谢所有评论。
Please let me know if what I am asking is not clear or if more information is needed, thanks again kindly for all the comments.
推荐答案
您的数据并不完全合理,因为它实际上并不工作perl代码。我猜测这是一个分成字符的字符串。之后,它听起来像你只是想能够找到最高频率的字符,这本质上只是一个 sort
按降序计数。
Your data doesn't entirely make sense, since it's not actually working perl code. I'm guessing that it's a string divided into characters. After that it sounds like you just want to be able to find the highest frequency character, which is essentially just a sort
by descending count.
因此,以下演示如何计算您的字符,然后对结果:
Therefore the following demonstrates how to count your characters and then sort the results:
use strict;
use warnings;
my $str = '------------------------------------------------------------------MGG-------------------------------------------------------------------------------------';
my @chars = split '', $str;
#Count Characteres
my %count;
$count{$_}++ for @chars;
delete $count{'-'}; # Don't count -
# Sort keys by count descending
my @keys = sort {$count{$b} <=> $count{$a}} keys %count;
for my $key (@keys) {
print "$key $count{$key}\n";
}
输出:
Outputs:
G 2
M 1
这篇关于选择元素的最高数量,除非......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!