use 5.010; use strict; use warnings; use List::Util 'max'; if (scalar(@ARGV) != 1) { print "\n"; print "Usage: monk.pl \n"; print "\n"; exit(); } # Read in the file my ($FILENAME) = @ARGV; open(DATA, $FILENAME); my (%data, %all, @keys); while () { chomp; my @chunks = split; my $key = shift @chunks; push @keys, $key unless exists $data{$key}; foreach my $chunk (@chunks) { $data{$key}{$chunk}++; $all{$chunk} = 1; } } #now make a file for the ouput my $outputfile = "organised_columns.txt"; if (! open(POS, ">>$outputfile") ) { print "Cannot open file \"$outputfile\" to write to!!\n\n"; exit; } my @fields = sort {$a <=> $b} keys %all; #my @fields = sort {lc($a) cmp lc($b)} keys %all; my $l = max map {length} @fields; foreach my $key (@keys) { while (keys %{$data{$key}}) { print POS $key, " "; foreach my $field (@fields) { if ($data{$key}{$field}) { printf POS "%${l}s ", $field; delete $data{$key}{$field} unless --$data{$key}{$field}; } else { printf POS "%${l}s ", "-"; } } print POS "\n"; } }