sandeepau 0 Newbie Poster

Hello,

I'm executing following Perl script program with purpose of comparing two text file & save output in third file with flags.

However, the execution process of this Perl script taking too much time for large size input text file. Can someone suggest performance optimization technique for following script.

1. #!/usr/bin/perl
2. use strict;
3. use warnings;
4.
5. my ($file1, $file2, $file3) = qw(1.txt 2.txt 3.txt);
6. open my $fh1, '<', $file1 or die "Can't open $file1: $!";
7. open my $fh2, '<', $file2 or die "Can't open $file2: $!";
8. open my $fh3, '>', $file3 or die "Can't open $file3: $!";
9.
10. my %save; #Hash of hashes to store records from file1 for comparison with file2
11. while (<$fh1>){
12. chomp;
13. my @rec = split /\|/;
14. my $key = $rec[2];
15. $save{$key}->{'data'} = $_; #Save current record in hash
16. $save{$key}->{'flag'} = 'D';
17. }
18.
19. while (<$fh2>){
20. chomp;
21. my @rec = split /\|/;
22. my $key = $rec[2];
23.
24. if (not exists $save{$key}){
25. $save{$key}->{'data'} = $_;
26. $save{$key}->{'flag'} = 'I';
27. }elsif (string_to_compare($_) ne string_to_compare($save{$key}->{'data'})){
28. $save{$key}->{'data'} = $_;
29. $save{$key}->{'flag'} = 'U';
30. }else{
31. delete $save{$key};
32. }
33. }
34.
35. foreach (sort keys %save){
36. print $fh3 "$save{$_}->{'data'}|$save{$_}->{'flag'}\n";
37. }
38.
39. sub string_to_compare{
40. my $line = shift;
41. my ($skip1, $skip2, $key, $remainder) = split /\|/, $line, 4;
42. return $remainder;
43. }

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.