#!usr/bin/perl use strict; use warnings; use IO::String; use List::Util 'max'; my $FILENAME4 = "test_data.txt"; open(DATA, $FILENAME4); #create arrays and hashes to store stuff my (%data, %all, @keys); while () { # avoid \n on last field chomp; #split the data into chunks my @chunks = split(/\s{2,}/, ); ## make sure you don't split inside annotated brackets #create keys for the chunks my $key = shift @chunks; #store the keys in an array unless they already exist push @keys, $key unless exists $data{$key}; foreach my $chunk (@chunks) { #return references using hashes $data{$key}{$chunk}++; #add all chunks to the hash '%all' $all{$chunk} = 1; } } #remove new_clusters2.txt if it exists my $remove2 = "new_clusters2.txt"; if (unlink($remove2) == 1) { print "Existing \"new_clusters2.txt\" file was removed\n"; } #now make a file for the ouput my $outputfile = "new_clusters2.txt"; if (! open(POS, ">>$outputfile") ) { print "Cannot open file \"$outputfile\" to write to!!\n\n"; exit; } #sort the fields/columns keys and save them as an array #my @fields = sort {$a <=> $b} keys %all; ##<--this sorting didn't work my @fields = sort {lc($a) cmp lc($b)} keys %all; #find the longest entry in an array my @array2 = (); foreach my $e (@fields){ #### my $d = $e; $d =~ m/(\S+)\_/; my $prefix = $1; # print "Prefix: ". $prefix."_"."\n\n"; ## prints the prefices push(@array2, $1); # print "* @array2 \n"; ## prints the prefices_ } #### my $longest = max map {length} @array2; #organise the data foreach my $key (@keys) { while (keys %{$data{$key}}) { print POS $key, " "; foreach my $field (@fields) { if ($data{$key}{$field}){ printf POS "%${longest}s ", $field; delete $data{$key}{$field} unless --$data{$key}{$field}; } else { printf POS "%${longest}s ", "-"; } } print POS "\n"; } }