我有两个文件,CUSTOMER_ACCOUNT_LOG.TXT,CUSOMER_ID_LOG.TXT。
这是日志,维护时间戳和帐户ID,与其他文件时间戳和customerid相同,
简单,我想选择匹配的TIMESTAMP的AccountID和CustomerID,
例如,123456793是TIMESTAMP,对于此Equlent匹配记录是ABC0103,CUSTOMER_ID_0103,
像这样我想选择详细信息并需要将这些匹配的记录写入另一个文件,
CUSOMER_ACCOUNT_LOG.TXT
TIMESTAMP| N1| N2 |ACCOUNT ID
-----------------------------------
123456789,111,1000,ABC0101
123456791,112,1001,ABC0102
123456793,113,1002,ABC0103
123456795,114,1003,ABC0104
123456797,115,1004,ABC0105
123456799,116,1005,ABC0106
123456801,117,1006,ABC0107
123456803,118,1007,ABC0108
123456805,119,1008,ABC0109
123456807,120,1009,ABC0110
123456809,121,1010,ABC0111
123456811,122,1011,ABC0112
123456813123,1012,ABC0113
123456815,124,1013,ABC0114
123456817,125,1014,ABC0115
123456819,126,1015,ABC0116
123456821,127,1016,ABC0117
123456823,128,1017,ABC0118
123456825,129,1018,ABC0119
123456827,130,1019,ABC0120
123456829,131,1020,ABC0121
CUSOMER_ID_LOG.TXT
TIMESTAMP| N1| N2 | CUSTOMER ID
-----------------------------------
123456789,111,1000,CUSTOMER_ID_0101
123456791,112,1001,CUSTOMER_ID_0102
123456793,113,1002,CUSTOMER_ID_0103
123456795,114,1003,CUSTOMER_ID_0104
123456797,115,1004,CUSTOMER_ID_0105
123456799,116,1005,CUSTOMER_ID_0106
123456801,117,1006,CUSTOMER_ID_0107
123456803,118,1007,CUSTOMER_ID_0108
123456805,119,1008,CUSTOMER_ID_0109
123456807,120,1009,CUSTOMER_ID_0110
123456809,121,1010,CUSTOMER_ID_0111
123456811,122,1011,CUSTOMER_ID_0112
123456813123,1012,CUSTOMER_ID_0113
123456815,124,1013,CUSTOMER_ID_0114
123456817,125,1014,CUSTOMER_ID_0115
123456819,126,1015,CUSTOMER_ID_0116
123456821,127,1016,CUSTOMER_ID_0117
123456823,128,1017,CUSTOMER_ID_0118
123456825,129,1018,CUSTOMER_ID_0119
123456827,130,1019,CUSTOMER_ID_0120
123456829,131,1020,CUSTOMER_ID_0121
我是一名PHP程序员,也是Perl的新手。
首先我读了这个文件,然后我就是maded array,现在我的数组中包含了所需的详细信息的时间戳,实际应该知道什么?我们应该将文件和fille值读入数组,所以猜测,数组键应该包含Account id和数组值应该是时间戳反之亦然不确定,就像另一个文件一样,最后我们应该比较时间戳,哪个时间戳匹配然后时间戳帐号id和客户ID我们应该选择,据我所知,我填充了数组,现在我不知道如何继续进行,因为,这里应该使用foreach然后需要匹配noth文件时间戳,我被困在这里!
答案 0 :(得分:9)
以下是我要采取的步骤:
0)一些基本的Perl样板(这是第0步,因为你应该总是总是这样做,有些人会在这个样板上添加其他东西,但这是最低限度的):
use strict;
use warnings;
use 5.010;
1)将第一个文件读入一个哈希,其密钥是时间戳:
my %account;
open( my $fh1, '<', $file1 ) or die "$!";
while( my $line = <$fh1> ) {
my @values = split ',', $line;
$account{$values[0]} = $values[3];
}
close $fh1;
2)读取第二个文件,每次读取一行时,拉出时间戳,然后将时间戳,帐户ID和客户ID打印到新文件中。
open( my $out_fh, '>', $outfile ) or die "$!";
open( my $fh2, '<', $file2 ) or die "$!";
while( my $line = <$fh2> ) {
my @values = split ',', $line;
say $out_fh join ',', $values[0], $account{$values[0]}, $values[3];
}
close $out_fh;
close $fh2;
您不希望将整个文件读入数组,因为这会浪费内存。仅存储您需要的信息,并利用Perl的数据类型来帮助您存储该信息。