如何也使用 Perl 哈希中键的替代字符作为键?
我的代码:
use strict;
use warnings;
my $seq = "ATGGT[TGA]G[TA]GC";
print "The sequences is $seq\n";
my %regex = (
AG => "R",
TC => "Y",
GT => "K",
AC => "M",
GC => "S",
AT => "M",
CGT => "B",
TGA => "D",
ACT => "H",
ACG => "V",
ACGT => "N"
);
$seq =~ s/\[(\w+)\]/$regex{$1}/g;
print "$seq\n";
我的理想输出是:ATGGTDGMGC 但在上面的场景中,由于我的哈希密钥是 AT 而不是 TA,因此它无法运行。解决这个问题的一种方法是添加另一个键值: TA => “M”。但我无法对所有键值对执行此操作,因为可能性太多。
那么有没有更好的方法来解决这个问题呢?
谢谢..
My code:
use strict;
use warnings;
my $seq = "ATGGT[TGA]G[TA]GC";
print "The sequences is $seq\n";
my %regex = (
AG => "R",
TC => "Y",
GT => "K",
AC => "M",
GC => "S",
AT => "M",
CGT => "B",
TGA => "D",
ACT => "H",
ACG => "V",
ACGT => "N"
);
$seq =~ s/\[(\w+)\]/$regex{$1}/g;
print "$seq\n";
My ideal output is: ATGGTDGMGC
But in the above scenario, since my hash key is AT and not TA, it doesn't run. One way to solve this problem would be adding another key-value: TA => "M". But I cannot do this for all key-value pairs, as there are too many possibilities.
So is there a better way to address this issue??
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜你的意思是括号中内容的顺序并不重要,因此
AT
相当于TA
,而TAG
相当于 < code>TGA 等[请注意,另一个 Eric 做出了不同的猜测。你不是很清楚自己想要什么。 ]
你可以对字母进行排序。
或者
I'm guessing you mean that the order of the stuff in brackets is unimportant, so
AT
is equivalent toTA
, andTAG
equivalent toTGA
, etc.[ Note that the other Eric made a different guess. You weren't very clear on what you wanted. ]
You could sort the letters.
or
Perl 无法知道键
AT
与TA
的含义相同,除非您以某种方式告诉它。如果所有序列都可以颠倒,那么您可以执行以下操作:您可能还应该检查以确保没有覆盖任何现有密钥。
或者,您可以修改正则表达式:
同样,这两个示例都假设您的所有密钥都可以反转。如果没有,那么您将必须手动输入冲销,或制定某种冲销选择标准。
Perl has no way of knowing that the key
AT
means the same thing asTA
unless you tell it in some way. If all of your sequences can be reversed, then you could do something like:You probably should also check to make sure you are not overwriting any existing keys.
Alternatively, you could modify the regex:
Again both of these examples assume that all of your keys can be reversed. If not, then you will have to either enter the reversals manually, or develop some sort of selection criteria for reversal.