Perl - 多个键并合并两个文件

时间:2013-09-17 23:58:20

标签: perl

#!/usr/bin/env perl

use strict;
use warnings;
use autodies;

#constants
use constant {
    FILE_A  => '/home/test/input_a.csv',
    FILE_B  => '/home/test/input_b.csv',
    FILE_C  => '/home/test/output.csv',
};

my %a_hash;

my @a_array;
my @b_array;
my @c_array;

open my $c_fh, "<", FILE_C;
open my $a_fh, "<", FILE_A;

while ( my $line = <$a_fh> ) {
    chomp $line;
    $line = /^(.+?);/; 
    $a_hash{$1} = 1; 
    print $c_fh, $line . "\n"; #populate a.csv into c.csv
}
close $a_fh;

open my $b_fh, "<", FILE_B;
#reading b.txt
while ( my $line = <$b_fh> ) {
    chomp $line;
    #your suggestion added
    $line = /^(.+?);/;
    if ( not exists $a_hash{$1} ) {
        print $c_fh, $line . "\n"; #populate a.csv into c.csv
    }
}
close $b_fh;
close $c_fh; 

错误讯息:

Use of uninitialized value $_ in pattern match (m//) at ./test.pl line 34, <$b_fh> line 1.
Use of uninitialized value $1 in exists at ./test.pl line 35, <$b_fh> line 1.

1 个答案:

答案 0 :(得分:0)

哈希是索引文件的绝佳方式。你说:

  

我需要从a.txt到c.txt的所有行。但是当从b.txt中选择行到c.txt时,首先我需要查看a.txt。如果该行已存在于a.txt中,那么在写入c.txt [output]时我不应该考虑b.txt行。

这意味着您只需要对a.txt中的行进行索引。您也没有提到计划如何将文件a.txt合并到c.txt中。你是从每个文件中读取一行吗?最终输出是否要进行排序?

而且,匹配线是什么意思?你的意思是整行匹配,还是只有第一个分号匹配?

为了保持灵活性,我将把所有行读入各种数组,并让你从那里解决问题。

  • 我们将a.txt读入一个数组并读入一个哈希值以进行索引。
  • 我们会将b.txt读入另一个数组,但我们会跳过哈希索引中的所有行。
  • 我们将把c.txt读入另一个数组。
  • 您可以使用这些数组执行所需操作,并根据需要合并行。

以下是该计划:

#! /usr/bin/env perl

# Preliminary stuff. The first two are always a must
use strict;
use warnings;
use autodies;   # No need to test on read/write or open/close failures

# This is how Perl defines constants. It's not great
# And unlike variables, they don't easily interpolate in 
# strings. But, this is what is native to Perl. There are 
# optional modules like "Readonly" that do a better job.
use constant {
    FILE_A  => 'a.txt',
    FILE_B  => 'b.txt',
    FILE_C  => 'c.txt',
};

# I'll use this for indexing
my %a_hash;

# I'll put the file contents in these three arrays
my @a_array;
my @b_array;
my @c_array;

open my $a_fh, "<", FILE_A;

# I'm reading each line of FILE_A. As I read it,
# I'll get the first field and put that as an index
# to my hash

while ( my $line ~= <$a_fh> ) {
    chomp $line;
    $line = /^(.+?);/;     # This strips the first field from the line
    $a_hash{$1} = 1;       # Now, I'll use the first field as my index to my hash
    push @a_array, $line;  # This adds the line to the end of the array
}
close $a_fh;

# I'll do the same for FILE_B as I did for FILE_A
# I'll go through line by line and push them into @b_array.
# One slight difference. I'll pull out the first field in
# my line, and see if it exists in my %a_hash where I indexed
# the lines in FILE_A. If that line does not exist in my %a_hash
# index, I'll push it into my @b_array

open my $b_fh, "<", FILE_B;
while ( my $line = <$b_fh> ) {
    $line ~= /^(.+?);/;
    if ( not exists $a_hash{$1} ) {
        push @b_array, $line;
    }
}
close $b_fh;

# Now, I'll toss all the lines in FILE_C into @c_array
# I can do a bit of a shortcut because I don't process
# the lines. I'll just put the whole file into @c_array
# in one fell swoop. I can use "chomp" to remove the NL
# from the end of each item of @c_array in a single line.

open my $c_fh, "<", FILE_C;
@c_array = <$c_fh>;
chomp @c_array;
close $c_fh;

# At this point, @a_array contains the entire contents of FILE_A
# in the order of that file. @c_array also contains all the lines in
# FILE_C in the order of that file. @b_array is a bit different, it
# also contains all of the lines in FILE_B **except for those lines
# whose first column were already in FILE_A.
# 
# You don't specify exactly what you want done at this point. Do
# you want to combine @a_array with @b_array? Here's how we can do
# that:

my @combined_array = sort (@a_array, @b_array);

现在,您有三个代表三个文件的数组,这三个数组的顺序与您的文件相同。

@a_array@c_array分别包含a.txtc.txt中的所有行。 @b_array包含b.txt中但不在a.txt中的所有行。

现在,您可以使用这三个数组并以您想要合并它们的方式合并它们。