2017年9月12日火曜日

PERL multi dimensional array

use strict;
use Text::CSV_XS;
my $csv = Text::CSV_XS->new ({ binary => 1 }); # open file w/ binary mode
# prepare array to store input file names;
# 2 arguments. no.1($ARGV[0]) is the original file and no.2($ARGV[1]) is the new file to compare each other.
my @inputfile = ($ARGV[0],$ARGV[1]);
my @origin;
my @new;

foreach my $count(@inputfile){
  # print "input file = $count\n"; # this line is for debug
  open my $fh, "<", $count; # case for foreach
  while (my $columns = $csv->getline ($fh)) {
    if($. == 1){ next;}  # skip the first line, as they are header info.
    #
    #  40 for cc addr, 21 for assignee, 21 for new alias(customer name), 0 for incident ID.
    #
    $$columns[40] =~ s/,/;/g; # replace comma with semi colong in the column no.40 CC history.
    $$columns[40] =~ s/ //g; # surpress all spaces in the line for future purpose.
    my @line;
    @line = ($$columns[21],$$columns[0],$$columns[12],$$columns[40]);
    if($count eq $ARGV[0]){ # arg no.0  is the name of original file.
      push @{$origin[$.-1]}, @line;  # store data into the multi dimension aray "origin" for original to compare.
    }
    if($count eq $ARGV[1]){ # arg no.1 is the name of new file.
      push @{$new[$.-1]}, @line; # into the buffer for new.
    }
  }
  close $fh;
  # print "close fh\n";  # this is for debug
}
my $size = @origin;
my $origin_size = @origin;
my $new_size = @new;
print "new size is ",$new_size,"\n";  # debug.
# print "file read end\n";
# foreach my $outbuf(@origin){
#   print @$outbuf[1],@$outbuf[2],@$outbuf[3],@$outbuf[4]," NL\n";
#
#   $size = @origin;
#   print "origin is ",$size,"\n";
# }
my $inc = 1;
my $alias = 2;
my $cc = 3;
my $assign = 0;
# debug output
# print "1 ",$origin[0][1],$origin[0][2],$origin[0][3],$origin[0][0]," NL\n";
# print $size," ",$origin[$size-1][1],$origin[$size-1][2],$origin[$size-1][3],$origin[$size-1][0]," NL\n";
# print "##################################\n";
# $size = @new;
# print "1 ",$new[0][1],$new[0][2],$new[0][3],$new[0][0]," NL\n";
# print $size," ",$new[$size-1][1],$new[$size-1][2],$new[$size-1][3],$new[$size-1][0]," NL\n";
print "############## start ####################\n";
# my $i = 1; my $j = 1;
for(my $i =0; $i <= $new_size-1; $i++){
  my $break = 0;  # initialize the flag if it is newly added in new or not.
  for(my $j=0; $j <= $origin_size-1; $j++){
    if($new[$i][$inc] eq $origin[$j][$inc]){  # all incident id in new is compared to the ones in origin.
        if($new[$i][$cc] ne $origin[$j][$cc]){ # when found the entry, compare cc history each other
          print "### unmatch ### ,";             # for the case there is delta.
        }else{
          print "### matched ### ,";             # or cc history is not updated at all.
        }
      print $new[$i][$assign],",",$new[$i][$inc],",\"",$new[$i][$alias],"\",",$new[$i][$cc],",=,",$origin[$j][$cc],"\n";
      last;   # exit if find the equal in the origin.
    }else{
      if($j == $origin_size -1){ # reached the final entry means this is new.
        print "### __new__ ### ,";
        print $new[$i][$assign],",",$new[$i][$inc],",\"",$new[$i][$alias],"\",",$new[$i][$cc],"\n";
      }
    }
  }
}
$csv->eof;

0 件のコメント: