Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
比较Perl中的CSV文件_Perl_Csv - Fatal编程技术网

比较Perl中的CSV文件

比较Perl中的CSV文件,perl,csv,Perl,Csv,我需要每天比较两个大型CSV文件: 文件1: "Record number";"€ price" "000001";"€ 19,95" "000002";"€ 20,50" 文件2: record number;price;date 000001;18,95;01-01-2014 000002;21,50;02-02-2014 文件1在每条记录后都包含换行符 文件2包含每条记录之后的换行和回车 我正在寻找一种在Perl中根据记录编号比较file1和file2的方法,并在一个单独的文件中的pr

我需要每天比较两个大型CSV文件:

文件1:

"Record number";"€ price"
"000001";"€ 19,95"
"000002";"€ 20,50"
文件2:

record number;price;date
000001;18,95;01-01-2014
000002;21,50;02-02-2014
文件1在每条记录后都包含换行符 文件2包含每条记录之后的换行和回车

我正在寻找一种在Perl中根据记录编号比较file1和file2的方法,并在一个单独的文件中的price列上打印差异。记录编号、价格和日期

希望任何人都能就如何处理这个问题提供一些建议


提前谢谢

我相信有更快捷、更有效的方法可以做到这一点,但下面应该是你描述的工作。我通过创建一个散列来比较这两个文件,因此我假设记录编号是唯一的。我已经对代码进行了注释,以解释每个步骤

#!/usr/bin/perl  

use strict;
use warnings;

#declare hashes and arrays
my %file1_price;
my %file2_price;
my %file2_date;
my @file1_records;

#open the two files with the data
open (FILE1,'</path/to/file1.txt');
open (FILE2,'</path/to/file2.txt');

#open a third file to write the data out to
open (FILE3,'>/write/to/new_file.txt');

#get rid of the headers in both files
my @file1 = (<FILE1>);
my $file1_header = shift(@file1);

my @file2 = (<FILE2>);
my $file2_header = shift(@file2);

#start a loop in file 1 and change the data format to compare to file 2
foreach my $file1_data (@file1){
chomp $file1_data;

my ($file1_record, $file1_price) = split(/;/,$file1_data);

# get rid of the quotes in both variables
$file1_record =~s/"//g;
$file1_price =~s/"//g;

#get rid of the € and substitute the comma for a decimal point 
$file1_price =~s/€//g;
$file1_price =~s/,/./g;

#create a hash for the price for later comparison
$file1_price{$file1_record} = $file1_price;

push @file1_records, $file1_record;

}

#start a loop in file 2 similar to the above loop ready to compare to file 1    
foreach my $file2_data (@file2){
chomp $file2_data;

my ($file2_record, $file2_price, $file2_date) = split(/;/,$file2_data);

#substitute the comma for a decimal point
$file2_price =~s/,/./g;

#create hashes of the data for final comparison below
$file2_price{$file2_record} = $file2_price;
$file2_date{$file2_record} = $file2_date;

}   

#final loop to compare both files' information  
foreach my $record (@file1_records){

#find the difference between the two prices 
my $difference = $file1_price{$record} - $file2_price{$record};

#print out to file
print FILE3 "$record, $file1_price{$record}, $file2_price{$record}, $difference,$file2_date{$record}\n";    

}

您使用的是什么版本的perl?请不要使用html格式化您的问题,而是使用标记,如果您不确定,请查看帮助。大多数与perl相关的CSV问题的答案中都包含Text::CSV或Text::csvx。你开始写东西了吗?$file1_price{$file1_record}=$file1_price;看起来并不完全正确。您正在将同一变量的旧值赋给自身。