Arrays Perl将数组值与数组散列中的值进行比较 这篇文章是根据DVK的评论和回答更新的

Arrays Perl将数组值与数组散列中的值进行比较 这篇文章是根据DVK的评论和回答更新的,arrays,perl,hash,Arrays,Perl,Hash,我已经改为使用3参数形式的open并正确地将id推入数组 更新代码如下 use warnings; use strict; use Data::Dumper; my $file1 = "Inputfile.txt"; my $file2 = $ARGV[0]; my $file3 = $ARGV[1]; open (OF, " > Results.txt") or die "Can't write new file: $!"; my %hash; open(INPUT1, "<

我已经改为使用3参数形式的open并正确地将id推入数组

更新代码如下

use warnings;
use strict;
use Data::Dumper;


my $file1 = "Inputfile.txt";
my $file2 = $ARGV[0];
my $file3 = $ARGV[1];

open (OF, " > Results.txt") or die "Can't write new file: $!";

my %hash;
open(INPUT1, "<" , $file1)or die("Failed to open file1: $!");
while (!eof(INPUT1)) {
    my @elements = split(/\t/, <INPUT1>);
    my $F1catagory = $elements[2];
    my $F1IDs = $elements[3];
    push @{$hash{$F1catagory}}, $F1IDs;
}
close(INPUT1);

my @array1;
open(INPUT2, "<" , $file2) or die ("Failed to open file2: $!");
while (!eof(INPUT2)) {
    my @line = split(/\t/, <INPUT2>);
    my $F2catagory = $line [0];
    my $F2IDs    = $line [1];
    push (@array1, $F2IDs);
}
print OF @array1;
close(INPUT2);

my @array2;
open(INPUT3, "<" , $file3) or die ("Failed to open file3: $!");
while (!eof(INPUT3)) {
    my @lines = split(/\t/, <INPUT3>);
    my $F3catagory     = $lines [0];
    my $F3IDs = $lines [1];
    push (@array2, $F3IDs );


}
print OF @array2;
close(INPUT3);
文件2包含第1列中的项目类别和第2列中的项目ID

CS1G2   1455
TM65    157378
PFN1    5216
HUL1    11100
ERI3    79033
PR12    57479
HIFN    55662
HNPD    3184
 HI2    28996
 LD1    84316
GRB2    2885
 AL6    84964
PCM1    5108
 ZN7    126208
MAK2    5605
BCL3    602
文件3与文件2相同,只是项目ID不同

我需要找出文件1中是否有任何进程同时包含文件2和文件3中的项目

我希望这能使问题更清楚

#######原始问题 我在写剧本时遇到了一些麻烦

我有一个包含流程信息的文件,我已将流程ID和项目ID读入数组散列,其中流程ID作为键,项目ID作为值(数组散列为单个流程中的多个项目)

我有两个项目ID数组 @F2IDs和@F3IDs

如果在同一进程中的@F2IDs和@F3IDs中有任何项目ID(在相同的%hash值中),我想确定它在同一进程中

到目前为止我有这个代码

use warnings;
use strict;
use Data::Dumper;

my $file1 = "Infile.txt";
my $file2 = $ARGV[0];
my $file3 = $ARGV[1];

open (OF, " > Results.txt") or die "Can't write new file: $!";


my %hash;
open(INPUT, $file1)or die("Failed to open file2");
while (!eof(INPUT)) {
    my @elements = split(/\t/, <INPUT>);
    my $F1catagory = $elements[2];
    my $F1IDs = $elements[3];
    push @{$hash{$catagory}}, $F1IDs;
}
close(INPUT);

open(INPUT, $file2) or die "Can't write new file: $!";
while (!eof(INPUT)) {
    my @line = split(/\t/, <INPUT>);
    my $F2catagory = $line [0];
    my @F2IDs    = $line [1];
}
close(INPUT);

open(INPUT, $file3) or die "Can't write new file: $!";
while (!eof(INPUT)) {
    my @lines = split(/\t/, <INPUT>);
    my @F3catagory     = $lines [0];
    my @F3IDs = $lines [1];
}
close(INPUT);    
有人对如何在perl中实现这一点有什么想法吗

提前谢谢你的帮助 问候

您有三个问题:

  • 通常,您的脚本在读取数据时似乎包含很多错误。我现在不谈这个,因为这不是你问的。例如,您需要在循环2/3中将ID推入数组,而不是分配给数组(您在第一个循环中正确地完成了这一操作)

  • 您的主要技术问题似乎是“如何判断哈希值(即数组引用)中是否存在某个ID?”

    最好的方法是将数据存储在ID的hashref(映射到“1”)中,而不是ID的arrayref中,并使用哈希查找:

    # This is in your first loop reading file 1
    # OLD CODE:    push @{$hash{$catagory}}, $F1IDs;
    # NEW CODE:
    # # Each hash value is a hashref, with keys being IDsx and values simply 1
    $hash{$catagory}->{$F1IDs} = 1; 
    
    # This is the code in the end, to check if some ID is in the hash:
    if ($hash{$catagory}->{$F1IDs}) {
        # $id_is_in_hash = 1;
    }
    
  • 坦白地说,您的伪代码对我来说(或者您的描述)没有太多意义,因此我无法帮助您编写一个完整的检查,使用我的解决方案替换您的伪代码。如果您可以提供更明确的描述,或者更好的例子数据在3个文件和所需的输出,我将能够做更多


  • 作为一个不相关的补充说明,您使用
    open
    的方式是不安全的,并且可能由于使用全局文件句柄而导致错误。请使用带词法文件句柄的三参数表单:
    open(my$input,"请您也给出输入数据和输出数据的内容以及预期结果的示例好吗?单词描述有点混乱。我编辑了m问题来解释这些评论。您能进一步帮助我吗?我想问题现在更清楚了。非常感谢您迄今为止的帮助。我很难理解关于perl。
    
    $insameprocess = False;
    foreach value in %hash;
        if F2IDs and F3IDs are in {$hash{$catagory}};;
           $insameprocess = True;
           print OF "the key the value and if they are in the same process";
    
    # This is in your first loop reading file 1
    # OLD CODE:    push @{$hash{$catagory}}, $F1IDs;
    # NEW CODE:
    # # Each hash value is a hashref, with keys being IDsx and values simply 1
    $hash{$catagory}->{$F1IDs} = 1; 
    
    # This is the code in the end, to check if some ID is in the hash:
    if ($hash{$catagory}->{$F1IDs}) {
        # $id_is_in_hash = 1;
    }