Reputation: 25733
I have two files, CUSTOMER_ACCOUNT_LOG.TXT, CUSOMER_ID_LOG.TXT.
Am this is log, maintain the timestamp and account id, same like in another file timestamp and customerid,
simple, i want to pick the AccountID and CustomerID with matched TIMESTAMP,
For example, 123456793 is TIMESTAMP, FOR this Equlent match records are ABC0103,CUSTOMER_ID_0103,
like this i want to pick detaild and need to make these matched records wrtite into another file,
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
I am a PHP programer, and new to Perl.
First i read the file, and then i just maded array, now my array contains the timestampe rest of the required details, actually what should do know ? we should read the file and fille values into array, so guess, array key should contain Account id and array value should timestamp wise versa not sure, same like another file, finally we should compare the time stamp, which timestamps are matched then timestamps account id and customer id we should pick, upto my knowledge i filled the array, now i dont knwo how to proceed further, because, here should use the foreach and then need to match noth file timestamps, am stuck here !
Upvotes: 1
Views: 1913
Reputation: 35828
Here are the steps I'd take:
0) Some rudimentary Perl boilerplate (this is step 0 because you should always always always do it, and some people will add other stuff to this boilerplate, but this is the bare minimum):
use strict;
use warnings;
use 5.010;
1) Read the first file into a hash whose keys are the timestamp:
my %account;
open( my $fh1, '<', $file1 ) or die "$!";
while( my $line = <$fh1> ) {
my @values = split ',', $line;
$account{$values[0]} = $values[3];
}
close $fh1;
2) Read the second file, and each time you read a line, pull out the timestamp, then print out the timestamp, the account ID, and the customer ID to a new file.
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;
You don't want to read the whole file into an array because that's a waste of memory. Only store the information that you need, and take advantage of Perl's datatypes to help you store that information.
Upvotes: 9