Guest User

Untitled

a guest
Jun 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. use Tie::Handle::CSV;
  6. use Data::Dumper;
  7.  
  8. our $foundZero = 0;
  9. my $currentPO = '';
  10. my $countCurrentPO = 0;
  11. my $debug = 1;
  12. my @amounts;
  13. my $fh = Tie::Handle::CSV->new('test2.csv', header => 1);
  14.  
  15. open MATCHES, ">matches.csv";
  16. open VARIANCES, ">variances.csv";
  17.  
  18. my @lines = <$fh>;
  19. undef my @matchedLines;
  20. print "FINDING MATCHES...\n";
  21. for (my $k = 0; $k < $#lines + 1; $k++) {
  22. $countCurrentPO++;
  23. print "k: " . $k . "\n" if $debug;
  24. my $csv_line = $lines[$k];
  25. my $debit = ($csv_line->{'DEBIT_AMOUNT'} eq '') ? 0 : $csv_line->{'DEBIT_AMOUNT'};
  26. my $credit = ($csv_line->{'CREDIT_AMOUNT'} eq '') ? 0 : $csv_line->{'CREDIT_AMOUNT'};
  27. my $po = $csv_line->{'PO_NUM'};
  28. my $total = $credit - $debit;
  29.  
  30. # print "matchedLines[k]: ". $matchedLines[$k - 1] . "\n" if defined($matchedLines[$k - 1]);
  31.  
  32. if (!(defined($matchedLines[$k]))) { # if the current line haven't been matched previously
  33.  
  34. if (@amounts < 1) {$currentPO = $po} # if it's the first one, it becomes the current PO
  35. print "PO/CURRENTPO: " . $po . "/" . $currentPO . " countCurrentPO: " . $countCurrentPO . "\n" if $debug;
  36. if ((($po eq $currentPO) || ($k == 0))) {
  37. # dont add a duplicate amount (with same row number)
  38. # my $temp = defined($amounts[0][1]) ? $amounts[0][1] : 6666666; # will never get 6666666 rows, so it should be safe..
  39. push(@amounts, [$total, $k]);# if ($temp != $k);
  40. if ($k == 77) {
  41. my $d = Data::Dumper->new(\@amounts);
  42. print $d->Dump;
  43. }
  44. if ($k == $#lines) {
  45. print "TOTAL1: " . $total . " I3: " . $k . "\n" if $debug;
  46. my $output = allSums_f2(\@amounts);
  47. print "OUTPUT3: " . $output . "\n" if $debug;
  48. my @numbers = split(/ /, $output);
  49. if ($foundZero == 1) {
  50. foreach(@numbers) {
  51. $matchedLines[$_] = 1;
  52. print "(1) SETTING NUMBER $_ AS MATCHED\n" if $debug;
  53. }
  54. $foundZero = 0;
  55. # $k = $k - $countCurrentPO;
  56. # $countCurrentPO = 0;
  57. }
  58. }
  59. } else { # this amount is part of a new PO, process old ones
  60. print "TOTAL2: " . $total . " I3: " . $k . "\n" if $debug;
  61. # if ($k == 77) {
  62. # my $d = Data::Dumper->new(\@amounts);
  63. # print $d->Dump;
  64. # }
  65. my $output = allSums_f2(\@amounts);
  66. print "OUTPUT4: $output \n" if $debug;
  67. my @numbers = split(/ /, $output);
  68. my $tempK = $k;
  69.  
  70. if ($foundZero == 1) {
  71. foreach(@numbers) {
  72. $matchedLines[$_] = 1;
  73. print "(2) matchedLines[_]: ". $matchedLines[$_] . "\n" if (( $matchedLines[$_] == 1) && $debug);
  74. print "(2) SETTING NUMBER $_ AS MATCHED\n" if $debug;
  75. }
  76. $foundZero = 0;
  77.  
  78. $k = $k - $countCurrentPO;
  79. print "tempK: " . $tempK . " K: " .$k . "\n" if $debug;
  80. @amounts = ();
  81. print "TOTAL: $total\n" if $debug;
  82. push(@amounts, [$total, $tempK]);
  83. $countCurrentPO = 1;
  84. # $currentPO = $po;
  85. }
  86. else {
  87. @amounts = ();
  88. push(@amounts, [$total, $k]);
  89. $countCurrentPO = 1;
  90. $currentPO = $po;
  91. }
  92. # print "DUMP1: ";
  93. # my $d = Data::Dumper->new(\@amounts);
  94. # print $d->Dump;
  95. }
  96. }
  97. }
  98.  
  99. print "WRITING RESULT FILES...\n";
  100. for (my $j = 0; $j < $#matchedLines + 1; $j++) {
  101. if (defined($matchedLines[$j])) {
  102. print MATCHES $lines[$j] . "\n";
  103. } else {
  104. print VARIANCES $lines[$j] . "\n";
  105. }
  106. }
  107.  
  108. close $fh;
  109.  
  110.  
  111. sub allSums_f2 {
  112. my($check_ref) = @_;
  113. my @check = @{$check_ref};
  114. my $desired = 0;
  115.  
  116. # foreach my $index (0..2**@check-1) {
  117. for (my $index = 0; $index <= 2**@check-1; $index++) {
  118. my $sum = 0;
  119. foreach my $pos (0..@check-1) {
  120. my $bit = ($index >> $pos) % 2;
  121. # my $d = Data::Dumper->new(\@check);
  122. # print $d->Dump;
  123. $sum += $bit * $check[$pos][0];
  124. }
  125. if ($sum == $desired) {
  126. my @combo;
  127. foreach my $pos (0..@check-1) {
  128. push @combo, $check[$pos][1] if ($index >> $pos) % 2;
  129. }
  130. if (@combo > 0) {
  131. print join " ", @combo, "\n";
  132. $foundZero = 1;
  133. return join(' ',@combo)
  134. }
  135. }
  136. }
  137. }
Add Comment
Please, Sign In to add comment