language: Perl (perl 5.16.2)
date: 912 days 22 hours ago
link:
visibility: private
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/perl
use strict;
my %hash;
while (my $line = <DATA>) {
        $line =~ s/\s*\z//;
        my @array = split /,/, $line;
 
        # get the key.
        my $key = shift @array;
 
        # see if the key is already seen.
        if(exists $hash{$key} ) {
                # get ref to previous record of this key.
                my $ref = $hash{$key};
 
                # print key.
                print "$key,";
 
                # a new array.
                my @new_array;
 
                # populate the new array.
                for(my $i=0;$i<=$#array;$i++) {
                        $new_array[$i] = $array[$i] - $$ref[$i];
                }
 
                # join the array elements with comma.
                print join",",@new_array;
                print "\n";
        }
 
        # add/replace the current array as value for the current key.
        $hash{$key} = \@array;
}
__END__
key1,1,2,3,4
key2,2,3,4,5
key1,11,22,33,44
key2,22,33,44,55
key1,111,222,333,444
key2,222,333,444,555