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"; } } #### 0 3850_1_12_00403 3850_1_12_01148 3850_1_12_01185 3850_1_12_01591 3850_1_12_01967 3850_1_12_01968 3850_1_2_00804 3850_1_2_01074 3850_1_3_00078 3850_1_3_01217 3850_1_3_01911 3850_1_3_02337 3850_2_11_00015 3850_2_11_00064 3850_2_11_00267 3850_2_11_00831 3850_2_11_01101 3850_2_11_01229 3850_2_11_02298 3850_2_12_00022 3850_2_12_00972 3850_2_12_00973 3850_2_12_01106 3850_2_12_01255 3850_2_12_01566 3850_2_12_01938 3850_2_1_00600 3850_2_1_00601 3850_2_1_00779 3850_2_1_00781 3850_2_1_01243 3850_2_1_01309 3850_2_1_01310 3850_2_1_01515 3850_2_1_01770 3850_2_2_00784 3850_2_2_00959 3850_2_2_01130 3850_2_2_01131 3850_3_10_00057 3850_3_10_00409 3850_3_10_01230 3850_3_10_01375 3850_3_12_00117 3850_3_12_00766 3850_3_12_01370 3850_3_12_01956 3850_3_1_00816 3850_3_1_00868 3850_3_1_01284 3850_3_1_01663 3850_3_1_01670 3850_3_1_01994 3850_3_1_02128 3850_3_5_01110 3850_3_5_01111 3850_3_5_01647 3850_3_6_00010 3850_3_6_00306 3850_3_6_01151 3850_3_6_01188 3850_3_6_01928 3850_3_7_00081 3850_3_7_00105 3850_3_7_00565 3850_3_7_01010 3850_3_7_01166 3850_3_8_00221 3850_3_8_01011 3850_3_8_01047 3850_3_8_01198 3850_3_9_00014 3850_3_9_00782 3850_3_9_00958 3850_3_9_01566 3850_3_9_01904 3850_6_12_00046 3850_6_12_01028 3850_6_12_01063 3850_6_12_01064 3850_6_2_01091 3850_6_2_01126 3850_6_2_01127 3850_6_2_01128 3850_6_2_01826 3850_6_3_00696 3850_6_3_01055 3850_6_3_02294 3850_6_4_00056 3850_6_4_00914 3850_6_5_00014 3850_6_5_01061 3850_6_6_00477 3850_6_6_00897 3850_6_6_01041 3850_6_7_00537 3850_6_7_00538 3850_6_7_01713 3850_7_10_01021 3850_7_10_01164 3850_7_10_01661 3850_7_11_00630 3850_7_11_01744 3850_7_12_00670 3850_7_12_01111 3850_7_12_01114 3850_7_12_01251 3850_7_12_01523 3850_7_1_00576 3850_7_1_00972 3850_7_1_01006 3850_7_1_01008 3850_7_2_00943 3850_7_2_01586 3850_7_3_00695 3850_7_3_01086 3850_7_3_01119 3850_7_3_01829 3850_7_4_01013 3850_7_4_01672 3850_7_5_01043 3850_7_5_01174 3850_7_6_00019 3850_7_6_00055 3850_7_6_00866 3850_7_6_01008 3850_7_6_01513 3850_7_7_00177 3850_7_7_00332 3850_7_7_00795 3850_7_8_01029 3850_7_8_01063 3850_7_8_01193 3850_7_8_01499 3850_7_8_01802 3850_7_8_01803 3850_7_9_01793 3850_8_10_00107 3850_8_10_00688 3850_8_10_01026 3850_8_10_01836 3850_8_11_00715 3850_8_11_00716 3850_8_11_01205 3850_8_11_01660 3850_8_12_00167 3850_8_12_00168 3850_8_12_01209 3850_8_1_01024 3850_8_1_01577 3850_8_2_00980 3850_8_2_01126 3850_8_3_00006 3850_8_3_00101 3850_8_3_00729 3850_8_3_01058 3850_8_3_01770 3850_8_3_01773 3850_8_4_00112 3850_8_4_01157 3850_8_4_01158 3850_8_4_01306 3850_8_9_00146 3850_8_9_00260 3850_8_9_01205 3850_8_9_01206 3850_8_9_01352 3850_8_9_01837 1 3850_1_12_00470 3850_1_12_01878 3850_1_12_01879 3850_1_12_01880 3850_1_12_02239 3850_1_12_02240 3850_1_2_00310 3850_1_2_01813 3850_1_2_01814 3850_1_2_02173 3850_1_3_00300 3850_1_3_02100 3850_2_11_02142 3850_2_12_00256 3850_2_12_00317 3850_2_12_02333 3850_2_12_02335 3850_2_1_00014 3850_2_1_00183 3850_2_1_00248 3850_2_1_02169 3850_2_2_00060 3850_2_2_00347 3850_2_2_00405 3850_2_2_02185 3850_2_2_02186 3850_2_2_02187 3850_3_10_00332 3850_3_10_00466 3850_3_10_01851 3850_3_10_02175 3850_3_10_02176 3850_3_12_00043 3850_3_12_00107 3850_3_12_00325 3850_3_12_00407 3850_3_12_00910 3850_3_12_02237 3850_3_1_00436 3850_3_1_02373 3850_3_1_02374 3850_3_1_02375 3850_3_5_01952 3850_3_6_00283 3850_3_6_01859 3850_3_6_01861 3850_3_6_02226 3850_3_6_02227 3850_3_7_00115 3850_3_8_00211 3850_3_8_00295 3850_3_8_01759 3850_3_8_01760 3850_3_8_02200 3850_3_9_00151 3850_3_9_00464 3850_3_9_01820 3850_3_9_01821 3850_3_9_02176 3850_3_9_02177 3850_6_12_00143 3850_6_12_00151 3850_6_12_02052 3850_6_12_02053 3850_6_2_00317 3850_6_2_02085 3850_6_3_01682 3850_6_3_01683 3850_6_3_02060 3850_6_3_02062 3850_6_4_00084 3850_6_4_01904 3850_6_5_01754 3850_6_5_02116 3850_6_5_02118 3850_6_6_01835 3850_6_7_02071 3850_7_10_00124 3850_7_10_00157 3850_7_10_01956 3850_7_10_01958 3850_7_10_01959 3850_7_11_02105 3850_7_12_00023 3850_7_12_00076 3850_7_12_01745 3850_7_12_02087 3850_7_12_02088 3850_7_1_00010 3850_7_1_00101 3850_7_1_00113 3850_7_1_02000 3850_7_1_02001 3850_7_2_00171 3850_7_2_01930 3850_7_2_01932 3850_7_3_01761 3850_7_3_02133 3850_7_3_02134 3850_7_4_00173 3850_7_4_00188 3850_7_4_01600 3850_7_4_02006 3850_7_5_00054 3850_7_5_02098 3850_7_5_02099 3850_7_6_00184 3850_7_6_00448 3850_7_7_00103 3850_7_7_02055 3850_7_7_02056 3850_7_8_01714 3850_7_8_01720 3850_7_8_02048 3850_7_8_02049 3850_7_9_02111 3850_8_10_00075 3850_8_10_00327 3850_8_10_02050 3850_8_11_00133 3850_8_11_01911 3850_8_11_01912 3850_8_11_02436 3850_8_12_00214 3850_8_12_00223 3850_8_12_02055 3850_8_1_00134 3850_8_1_00181 3850_8_1_00238 3850_8_1_01576 3850_8_1_01824 3850_8_1_01825 3850_8_2_02040 3850_8_3_00223 3850_8_3_02075 3850_8_3_02076 3850_8_4_00179 3850_8_4_00189 3850_8_4_00246 3850_8_4_02182 3850_8_4_02183 3850_8_4_02184 3850_8_9_00233 3850_8_9_01835 3850_8_9_02200