【发布时间】:2012-04-04 23:03:51
【问题描述】:
我想生成一个唯一 ID 列表。因为有些 ID 是重复的,所以我需要在末尾添加一个数字以使其唯一,如下所示:
ID=exon00001
ID=exon00002
ID=exon00003
ID=exon00004
这是我目前所拥有的。
while (loop through the IDs) {
# if $id is an exon, then increment the counter by one and add it
# to the end of the ID
if ($id =~ m/exon/) {
my $exon_count = 0;
my @exon = $exon_count++; #3
$number = pop @exon; # removes the first element of the list
$id = $id.$number;
print $id."/n"
}
}
基本上我想用计数器动态生成一个数组。它应该为外显子的总数创建一个数组 (1, 2, 3, 4, ...),然后删除元素并将其添加到字符串中。此代码无法正常工作。我认为第 3 行有问题。你们知道吗?有任何想法吗?谢谢
【问题讨论】:
-
我的 $exon_count 在循环内并且每次都设置为零。将声明移动到循环之前。然后它将通过循环递增。另外,我会直接使用 $exon_count 而不是做所有的工作来将它分配给一个数组,然后将它放入数字中,或者只是使用数字并增加它。
-
你的代码充满了错误,即使它可以编译,它也不会按照你的想法去做。例如:
$exon_count在每次找到新的外显子时都会重置,您将单个值(始终为 0,因为之后评估 ++)分配给数组,pop删除 last array 的元素,"/n"将打印一个斜杠和n,如果你想要换行,你需要"\n"。 -
为了补充这些家伙所说的,
shift从列表中删除了 first 元素,pop删除了 last--然而它确实删除了堆栈的“顶部”元素,但这是一个 stack,而不是 list。
标签: arrays perl while-loop unique counter