Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Compare the values of the second column of two .csv files
- # Using the first column as key
- # Ignoring keys which only exist in second file
- # Result .csv contains new and old values for each key,
- # and additionally the percentual in- or decrease in a fourth column
- use constant {
- BASE_DIR => "G:\\MY_BASE_DIRECTORY\\",
- SEP => "\t" # Separator used in .csv files
- };
- use Text::CSV qw( csv );
- my $fnew = read_csv( "kw-49-2020.csv" );
- my $fold = read_csv( "kw-47-2020.csv" );
- while (my ($key, $new) = each %$fnew) {
- print "$key".SEP."$new";
- if (my $old = $fold->{$key}) {
- print SEP."$old";
- my $trend = 100 * ( $new - $old ) / $old;
- printf( SEP."%.2f",$trend );
- }
- print "\n";
- }
- sub read_csv {
- my $aoa = csv (in => BASE_DIR . shift, sep_char=> SEP );
- my %stats = ();
- for my $line (@$aoa) {
- my @record = @$line;
- my $key = ( $record[0] =~ s/^\s*(\w+)\s*/$1/r );
- $stats{$key} = $record[1];
- }
- return \%stats;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement