Advertisement
cd62131

JoinCSV

Aug 12th, 2014
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.53 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use feature qw(say);
  5. open my $csv1, $ARGV[0] or die;
  6. open my $csv2, $ARGV[1] or die;
  7. my $ret = {};
  8. while (<$csv1>) {
  9.   chomp;
  10.   my @d = split /\s*,\s*/;
  11.   $ret->{$d[0]} = [$d[1]];
  12. }
  13. while (<$csv2>) {
  14.   chomp;
  15.   my @d = split /\s*,\s*/;
  16.   if ($ret->{$d[0]}) {
  17.     push $ret->{$d[0]}, $d[1];
  18.   } else {
  19.     $ret->{$d[0]} = [$d[1]];
  20.   }
  21. }
  22. close $csv1;
  23. close $csv2;
  24. foreach my $r (sort keys %$ret) {
  25.   next unless @{$ret->{$r}} == 2;
  26.   say "$r," . join ',', @{$ret->{$r}};
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement