+ -
当前位置:首页 → 问答吧 → 如何通过哈希表查询A文件中的查询信息在B文件中的具体内容

如何通过哈希表查询A文件中的查询信息在B文件中的具体内容

时间:2010-11-18

来源:互联网

我有文件A和B
其中A中内容为
gi|20178371|ref|NP_619791.1|
gi|20178373|ref|NP_619794.1|
gi|20178378|ref|NP_619799.1|
gi|20178380|ref|NP_619801.1|
gi|20178381|ref|NP_619802.1|
B为
gi|20178371|ref|NP_619791.1|   gi|9627716|ref|NP_042238.1|
gi|20178371|ref|NP_619792.1|   gi|9627716|ref|NP_042232.1|
gi|20178371|ref|NP_619793.1|   gi|9627716|ref|NP_323443.1|
gi|20178373|ref|NP_619794.1|   gi|9627716|ref|NP_566666.1|
gi|20178378|ref|NP_619799.1|   gi|9627716|ref|NP_042111.1|
gi|20178378|ref|NP_619800.1|   gi|9627716|ref|NP_042111.1|
gi|20178380|ref|NP_619801.1|   gi|9627716|ref|NP_342321.1|
gi|20178381|ref|NP_619802.1|   gi|9627716|ref|NP_042132.1|

我想得到如下的结果
gi|20178371|ref|NP_619791.1|   gi|9627716|ref|NP_042238.1|
gi|20178373|ref|NP_619794.1|   gi|9627716|ref|NP_566666.1|
gi|20178378|ref|NP_619799.1|   gi|9627716|ref|NP_042111.1|
gi|20178380|ref|NP_619801.1|   gi|9627716|ref|NP_342321.1|
gi|20178381|ref|NP_619802.1|   gi|9627716|ref|NP_042132.1|

#useage: perl x1.pl A B
open (F1,"$ARGV[0]");
open (F2,"$ARGV[1]");
open (F3,">gi_name");
select F3;
while (<F1>) {
        if (/^gi\|(\d+)\|re/) {
                $hash{$1}=$_;
        }
}
while ($gi=<F2>) {
        if ($gi =~ /^gi\|(\d+)\|re/) {
                if (exists $hash{$1}) {
                        print $hash{$1};
                }
        }
}

但我的程序的结果是这样的
gi|20178371|ref|NP_619791.1|
gi|20178373|ref|NP_619794.1|
gi|20178378|ref|NP_619799.1|
gi|20178380|ref|NP_619801.1|
gi|20178381|ref|NP_619802.1|   gi|9627716|ref|NP_042132.1|

我想请问下为什么前几个号的对应信息没有提出来,万分感谢,新手,不是很懂,刚刚学!

作者: sunbaofa   发布时间: 2010-11-18

#useage: perl x1.pl A B
open (F1,"$ARGV[0]");
open (F2,"$ARGV[1]");
open (F3,">gi_name");
select F3;
while (<F1>) {
        if (/^gi\|(\d+)\|re/) {
                $hash{$1}=$_;
        }
}
while ($gi=<F2>) {
        if ($gi =~ /^gi\|(\d+)\|re/) {
                if (exists $hash{$1}) {
                        print $hash{$1};
                }
        }
}
这个是我编的一个程序

作者: sunbaofa   发布时间: 2010-11-18

明白问题出哪了,$hash{$1}中本身就只有1列,请教高手这样的提取如何实现,不知道怎么改程序,我把A文件和B文件
#useage: perl x1.pl A B

换成
#useage: perl x1.pl  B A结果也不对,只显示A文件中的号对应的最后一行内容
,不能提取全部信息。

作者: sunbaofa   发布时间: 2010-11-18

  1. #!/usr/bin/perl
  2. #useage: perl x1.pl A B

  3. use strict;
  4. use warnings;

  5. my %hash;
  6. open (F1,"$ARGV[0]");
  7. while (<F1>) {
  8.         chomp;
  9.          $hash{$_} = 1;
  10. }
  11. close F1;

  12. open (F3,">gi_name");
  13. open (F2,"$ARGV[1]");
  14. while (<F2>) {
  15.          my ($key,$value) = split /\s+/;
  16.          if (exists $hash{$key}) {
  17.             print F3 "$_";
  18.          }
  19. }
  20. close F2;
  21. close F3;

  22. <STDIN>;
复制代码
看楼主的要求,貌似应该这样写!

作者: iamlimeng   发布时间: 2010-11-18

本帖最后由 zhlong8 于 2010-11-18 21:11 编辑

是要文件2中的前两列数字和文件1中一样的就输出吗?
  1. #useage: perl x1.pl A B
  2. open (F1,"$ARGV[0]");
  3. open (F2,"$ARGV[1]");
  4. open (F3,">gi_name");
  5. select F3;
  6. while (<F1>) {
  7.     chomp;
  8.     $hash{$1} = 1;
  9. }
  10. while ($gi=<F2>) {
  11.         if ($gi =~ /^(gi|\d+|ref|NP_\d+\.\d|)/) {
  12.                 if (exists $hash{$1}) {
  13.                         print;
  14.                 }
  15.         }
  16. }
复制代码

作者: zhlong8   发布时间: 2010-11-18

是要文件2中的前两列数字和文件1中一样的就输出吗?

只要第1列相同就行,就相当于A是一个检索条目,要在B中检索到它所对应的所有内容

作者: sunbaofa   发布时间: 2010-11-18

非常感谢2位,我在我原来程序上改了一下
#useage: perl x1.pl A B
open (F1,"$ARGV[0]";
open (F2,"$ARGV[1]";
open (F3,">gi_name";
select F3;
while (<F1> {
        if (/^gi\|(\d+)\|re/) {
                $hash{$1}=$_;
        }
}
while ($gi=<F2> {
        if ($gi =~ /^gi\|(\d+)\|re/) {
                if (exists $hash{$1}) {
                        print $gi};
                }
        }
}


最后1句命令改成print $gi就可以了,是自己没注意,打另一个文件的内容了。看了两位的程序想起自己问题了,呵呵,以后多像前辈学习!

作者: sunbaofa   发布时间: 2010-11-18

回复 sunbaofa

请楼主把需求描述清楚,按你的逻辑,就互相矛盾了。若只是按第一列的数值来判断,那你在1楼给出的结果就是错的。我看你在1楼的结果,貌似可以用A文件的行来进行判断。

晕了...

作者: iamlimeng   发布时间: 2010-11-18

实在不好意思,A文件确实是只有1行的,用行来找是对的。是我刚才没解释清楚,不好意思啊。

作者: sunbaofa   发布时间: 2010-11-18

热门下载

更多