Re: Comparing 2 Columns



Another version of the program.  Output is more dense, less sparse.

Input is:

red     
green   white
black   gray
blue    black
white   cyan
        magenta
        yellow

Output is:

blue    cyan    black
green   gray    white
red     magenta 
        yellow  


#! /usr/bin/perl -CS

use warnings;
use strict;
use Text::CSV 'csv';
use File::Temp;
use List::Util 'max';

main: {
  my $ifile = 'csv-comm.gnumeric';
  my $sheet = 'Sheet1';
  my $dir = File::Temp->newdir(TEMPLATE => "temp-XXXXXXXX");
  system "ssconvert -S $ifile $dir/csv-comm-%s.csv";
  my $aoa = csv(in => "$dir/csv-comm-$sheet.csv");
  my %rslt;
  for my $col (0, 1) {
    my $flag = 1<<$col;
    checker: for my $row ($aoa->@*) {
      my $val = $row->[$col];
      next checker if $val eq '';
      $rslt{$val} += $flag;
    }
  }
  my @dense = ();       ## an array of array-references
  for my $val (sort keys %rslt) {
    my $which = $rslt{$val};
    push $dense[$which-1]->@*, $val;
  }
  my $top = max(map {0+$_->@*} @dense);
  for (my $row = 0; $row < $top; $row++) {
    for (my $col = 0; $col < 3; $col++) {
      print shift($dense[$col]->@*) // '', ",";
    }
    print "\n";
  }
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]