#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Sum a field where duplicates row values


    Hi My current code will compare two files and display the contents of specific fields in both files.
    But it wil not show the values if a field duplicates. Now it will display only the last value of that particular field, which is used to compare rest of values.

    I want to sum the count field if the fields in a particular row duplicates.
    If the field values are different, should display the duplicate field.

    Code:


    use strict;
    use warnings;
    my ($old_file, $new_file);

    ($old_file, $new_file) = get_args (@ARGV);
    sub get_args {
    my (@args) = @_;

    if ($#args != 1) {
    print "Usage: perl $0 old_file new_file\n";
    exit;
    }

    print "\n\nOld license file: $args[0]\n\n";
    print "New license file: $args[1]\n\n";

    return ($args[0], $args[1]);
    }



    #Hash of hash references
    my %HoH;

    #Read Line
    while (my $line = <>){
    #$line =~ s/^\s+//;

    #Remove space characters and tabs (if any) from start of line
    chomp $line;


    next unless $line =~ m/^LINE|^FIELD /;
    my $oldnew;

    if ($ARGV eq $old_license){
    $oldnew = 'old';
    }
    else{
    $oldnew = 'new';
    }

    my @fields = split /\s+/, $line;
    my $key = $fields[1];

    $HoH{$key} = {} if not exists $HoH{$fields[1]};
    #$HoH{$key}{'version'} = $fields[3];
    $HoH{$key}{$oldnew}{'version'} = $fields[3];
    $HoH{$key}{$oldnew}{'date'} = $fields[4];
    $HoH{$key}{$oldnew}{'num'} = $fields[5];
    }

    #Print Headers
    printf "\n%-30s%10s%11s%10s%8s%10s%13s\n","###############################", "#########", "#########", "########", "############","#########", "############";
    #printf "\n%-30s%10s%11s%10s%8s%10s%13s\n","===============================", "=========", "=========", "========", "============","=========", "============";
    printf "\n%-30s%10s%11s%12s%10s%10s%13s\n", "FEATURE","OLD_VER","NEW_VER","OLD_COUNT","NEW_COUNT","OLD_DATE","NEW_DATE";
    #printf "\n%-30s%10s%11s%10s%8s%10s%13s\n","===============================", "=========", "=========", "========", "============","=========", "============";
    printf "\n%-30s%10s%11s%10s%8s%10s%13s\n","###############################", "#########", "#########", "########", "############","#########", "############";


    #Print Compared Values
    foreach my $k (sort keys %HoH){
    #my $ver = $HoH{$k}{'version'};
    my $o_ver = exists $HoH{$k}{'old'}{'version'} ? $HoH{$k}{'old'}{'version'}: 'NONE';
    my $n_ver = exists $HoH{$k}{'new'}{'version'} ? $HoH{$k}{'new'}{'version'}: 'NONE';
    my $o_num = exists $HoH{$k}{'old'}{'num'} ? $HoH{$k}{'old'}{'num'}: 'NONE';
    my $n_num = exists $HoH{$k}{'new'}{'num'} ? $HoH{$k}{'new'}{'num'}: 'NONE';
    my $o_dt = exists $HoH{$k}{'old'}{'date'} ? $HoH{$k}{'old'}{'date'}: 'NONE';
    my $n_dt = exists $HoH{$k}{'new'}{'date'} ? $HoH{$k}{'new'}{'date'}: 'NONE';
    printf "\n%-30s%10s%11s%10s%7s%17s%13s\n", $k,$o_ver,$n_ver,$o_num,$n_num,$o_dt,$n_dt;
    }

    Input:

    old_file

    LINE ABC thank 2011.12 31-jan-2012 15 HGDFD!/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    HJDKDK JDJDJD DJKDKD DKDLAKA DKAJLKJLJLK DNALE"

    LINE ABC thank 2011.12 31-jan-2012 5 GDFDFDF/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    9A42 66F4 CD2B F6CA DBF6 F772 6D8E 5653 9ECC 05C9 A48E"

    LINE ABC thank 2011.12 31-oct-2012 5 DSFDB/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    9A42 66F4 CD2B F6CA DBF6 F772 6D8E 5653 9ECC 05C9 A48E"

    LINE DEF thank 2011.12 31-jan-2014 50 DSFDB/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    9A42 66F4 CD2B F6CA DBF6 F772 6D8E 5653 9ECC 05C9 A48E"

    new_file

    LINE ABC thank 2011.12 31-oct-2012 15 HGDFD!/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    HJDKDK JDJDJD DJKDKD DKDLAKA DKAJLKJLJLK DNALE"

    LINE ABC thank 2011.12 31-oct-2012 15 GDFDFDF/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    9A42 66F4 CD2B F6CA DBF6 F772 6D8E 5653 9ECC 05C9 A48E"

    LINE ABC thank 2011.12 31-jan-2014 10 DSFDB/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    9A42 66F4 CD2B F6CA DBF6 F772 6D8E 5653 9ECC 05C9 A48E"

    LINE DEF thank 2011.12 31-jan-2014 50 DSFDB/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    9A42 66F4 CD2B F6CA DBF6 F772 6D8E 5653 9ECC 05C9 A48E"

    OUTPUT:

    FEATURE | OLD_VER | NEW_VER | OLD_COUNT | NEW_COUNT OLD_DATE | NEW_DATE
    ABC 2011.12 2011.12 20 30 31-jan-2012 31-oct-2012
    ABC 2011.12 2011.12 5 10 31-oct-2012 31-jan-2014
    DEF 2011.12 2011.12 50 50 31-jan-2014 31-jan-2014
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,923
    Rep Power
    1225
    Please use the code tags when posting more than 2 lines of code. It makes it much easier for use to read your code.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Posts
    3
    Rep Power
    0
    Hi My current code will compare two files and display the contents of specific fields in both files.
    But it wil not show the values if a field duplicates. Now it will display only the last value of that particular field, which is used to compare rest of values.

    I want to sum the count field if the fields in a particular row duplicates.
    If the field values are different, should display the duplicate field.

    Code:


    Code:
    use strict;
    use warnings;
    my ($old_file, $new_file);
    
    ($old_file, $new_file) = get_args (@ARGV);
    sub get_args {
    my (@args) = @_;
    
    if ($#args != 1) {
    print "Usage: perl $0 old_file new_file\n";
    exit;
    }
    
    print "\n\nOld license file: $args[0]\n\n";
    print "New license file: $args[1]\n\n";
    
    return ($args[0], $args[1]);
    }
    
    
    
    #Hash of hash references
    my %HoH;
    
    #Read Line
    while (my $line = <>){
    #$line =~ s/^\s+//;
    
    #Remove space characters and tabs (if any) from start of line
    chomp $line;
    
    
    next unless $line =~ m/^LINE|^FIELD /;
    my $oldnew;
    
    if ($ARGV eq $old_license){
    $oldnew = 'old';
    }
    else{
    $oldnew = 'new';
    }
    
    my @fields = split /\s+/, $line;
    my $key = $fields[1];
    
    $HoH{$key} = {} if not exists $HoH{$fields[1]};
    #$HoH{$key}{'version'} = $fields[3];
    $HoH{$key}{$oldnew}{'version'} = $fields[3];
    $HoH{$key}{$oldnew}{'date'} = $fields[4];
    $HoH{$key}{$oldnew}{'num'} = $fields[5];
    }
    
    #Print Headers
    printf "\n%-30s%10s%11s%10s%8s%10s%13s\n","###############################", "#########", "#########", "########", "############","#########", "############";
    #printf "\n%-30s%10s%11s%10s%8s%10s%13s\n","===============================", "=========", "=========", "========", "============","=========", "============";
    printf "\n%-30s%10s%11s%12s%10s%10s%13s\n", "FEATURE","OLD_VER","NEW_VER","OLD_COUNT","NEW_COUNT","OLD_DATE","NEW_DATE";
    #printf "\n%-30s%10s%11s%10s%8s%10s%13s\n","===============================", "=========", "=========", "========", "============","=========", "============";
    printf "\n%-30s%10s%11s%10s%8s%10s%13s\n","###############################", "#########", "#########", "########", "############","#########", "############";
    
    
    #Print Compared Values
    foreach my $k (sort keys %HoH){
    #my $ver = $HoH{$k}{'version'};
    my $o_ver = exists $HoH{$k}{'old'}{'version'} ? $HoH{$k}{'old'}{'version'}: 'NONE';
    my $n_ver = exists $HoH{$k}{'new'}{'version'} ? $HoH{$k}{'new'}{'version'}: 'NONE';
    my $o_num = exists $HoH{$k}{'old'}{'num'} ? $HoH{$k}{'old'}{'num'}: 'NONE';
    my $n_num = exists $HoH{$k}{'new'}{'num'} ? $HoH{$k}{'new'}{'num'}: 'NONE';
    my $o_dt = exists $HoH{$k}{'old'}{'date'} ? $HoH{$k}{'old'}{'date'}: 'NONE';
    my $n_dt = exists $HoH{$k}{'new'}{'date'} ? $HoH{$k}{'new'}{'date'}: 'NONE';
    printf "\n%-30s%10s%11s%10s%7s%17s%13s\n", $k,$o_ver,$n_ver,$o_num,$n_num,$o_dt,$n_dt;
    }
    Input:

    old_file

    Code:
    LINE ABC thank 2011.12 31-jan-2012 15 HGDFD!/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    HJDKDK JDJDJD DJKDKD DKDLAKA DKAJLKJLJLK DNALE"
    
    LINE ABC thank 2011.12 31-jan-2012 5 GDFDFDF/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    9A42 66F4 CD2B F6CA DBF6 F772 6D8E 5653 9ECC 05C9 A48E"
    
    LINE ABC thank 2011.12 31-oct-2012 5 DSFDB/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    9A42 66F4 CD2B F6CA DBF6 F772 6D8E 5653 9ECC 05C9 A48E"
    
    LINE DEF thank 2011.12 31-jan-2014 50 DSFDB/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    9A42 66F4 CD2B F6CA DBF6 F772 6D8E 5653 9ECC 05C9 A48E"
    new_file
    Code:
    LINE ABC thank 2011.12 31-oct-2012 15 HGDFD!/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    HJDKDK JDJDJD DJKDKD DKDLAKA DKAJLKJLJLK DNALE"
    
    LINE ABC thank 2011.12 31-oct-2012 15 GDFDFDF/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    9A42 66F4 CD2B F6CA DBF6 F772 6D8E 5653 9ECC 05C9 A48E"
    
    LINE ABC thank 2011.12 31-jan-2014 10 DSFDB/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    9A42 66F4 CD2B F6CA DBF6 F772 6D8E 5653 9ECC 05C9 A48E"
    
    LINE DEF thank 2011.12 31-jan-2014 50 DSFDB/
    29-sep-2010 1B78 65E4 0F74 3DD1 67F6 09E2 F4A8 \
    9A42 66F4 CD2B F6CA DBF6 F772 6D8E 5653 9ECC 05C9 A48E"
    OUTPUT:

    Code:
    FEATURE | OLD_VER | NEW_VER | OLD_COUNT | NEW_COUNT OLD_DATE | NEW_DATE
    ABC 2011.12 2011.12 20 30 31-jan-2012 31-oct-2012
    ABC 2011.12 2011.12 5 10 31-oct-2012 31-jan-2014
    DEF 2011.12 2011.12 50 50 31-jan-2014 31-jan-2014

IMN logo majestic logo threadwatch logo seochat tools logo