It writes the two sets of data into an array each split by the endline character. Then goes through line by line and compares them using the 'cid' and 'date' sections at the beginning of each line i.e. 101212::20041106::, and then checks whether the 'avs' i.e. YYYYYYY is the same in each file where the 'cid' and 'date' are the same. Hope that makes sense!101210::20041009::YYYYYYY::::1096930741::_1_ 101210::20041016::YYYYYYY::::1096930741::_1_ 101210::20041023::NNNNNNN::::1096930741::_1_ 101210::20041030::NNNNNNN::::1096930741::_1_ 101210::20041106::NNNNNNN::::1096930741::_1_ 101212::20041009::NNNNNNN::::1096930741::_2_ 101212::20041016::NNNNNNN::::1096930741::_2_ 101212::20041023::NNNNNNN::::1096930741::_2_ 101212::20041030::YYYYYYY::::1096930741::_2_ 101212::20041106::YYYYYYY::::1096930741::_2_
sub compare_avs { # find the name of the file created at last running open(DIFF_OLD, $path.$agent."/AVS/DIFF_OLD.txt") || die "ERROR: Unable + to open ".$agent." - DIFF_OLD_FILE.txt"; $DIFF_OLD = <DIFF_OLD>; close DIFF_OLD; # check if file exists if not, write contents of new file to $ +data if ($DIFF_OLD eq "") { print "DIFF_OLD file - Does not exist\n"; open(DIFF, "$EMAILfile") || die "ERROR: Unable to open ".$agen +t." - ".$EMAILfile; while(my @AVSn = split/\n/, <DIFF>) { foreach $AVn(@AVSn) { $data = $data.$AVn."\n"; } } close DIFF; } else { print "DIFF_OLD file - ".$DIFF_OLD."\n"; sleep(5); # write contents of old file to an array open(CHECK, $path.$agent."/AVS/".$DIFF_OLD) || die "ERROR: Un +able to open ".$agent." - ".$DIFF_OLD; @AVSo = <CHECK>; close CHECK; # write contents of new file to array and loop through it open(DIFF, "$EMAILfile") || die "ERROR: Unable to open + ".$agent." - ".$EMAILfile; while(my @AVSn = split/\n/, <DIFF>) { foreach $AVn(@AVSn) { $AVSchecked = 0; ($SIDn,$DATEn,$AVSn,$NOTHINGn,$EPOCHDATEn,$SERIALn) = +split/::/,$AVn; # compare contents of old list with new list foreach $AVo(@AVSo) { $AVo =~ s/\n|\r//g; ($SIDo,$DATEo,$AVSo) = split/::/,$AVo; if ($SIDn eq $SIDo && $DATEn eq $DATEo && $AVSn ne + $AVSo) { $data = $data.$SIDn."::".$DATEn."::".$AVSn.":: +::".$EPOCHDATEn."::".$SERIALn."\n"; print $SIDn."::".$DATEn."::".$AVSn."::::".$EPO +CHDATEn."::".$SERIALn."\n"; $AVSchecked = 1; } elsif ($SIDn eq $SIDo && $DATEn eq $DATEo && $ +AVSn eq $AVSo) { $AVSchecked = 1; } } if ($AVSchecked == 0) { $data = $data.$SIDn."::".$DATEn."::".$AVSn."::::". +$EPOCHDATEn."::".$SERIALn."\n"; print $SIDn."::".$DATEn."::".$AVSn."::::".$EPOCHDA +TEn."::".$SERIALn."\n"; } } } close DIFF; } $DIFF_OLD_filex = $path.$agent."/AVS/DIFF_OLD.txt"; open(DIFF_OLD,">".$path.$agent."/AVS/DIFF_OLD.txt") || die "ERROR: + Unable to open ".$agent." - DIFF_OLD_FILE.txt to update"; print DIFF_OLD $agent.$$.".AVS"; close DIFF_OLD; }
In reply to Array loop is very inefficient, 100% CPU usage by waiterm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |