in reply to Re: Sorting issue
in thread Sorting issue

Hi Grandfather,

I forgot to post my input and desired output format. Sorry!

#Input file Tags Frequency EEBBBBGGGBB 1700 BBBCDDERFGG 850 CCCDEDFFFES 45 ----------- -- #output file Header Tags Frequency >HWTI_1700_468983 EEBBBBGGGBB 1700 >HWTI_850_52 BBBCDDERFGG 850 ------------

With my code, I am able to sort it by Tags, but I want to sort by Frequency. Though, I tried the suggestion by "aaron_baugher", I am getting warning "use of uninitiated value..."

Replies are listed 'Best First'.
Re^3: Sorting issue
by aaron_baugher (Curate) on Nov 05, 2011 at 00:31 UTC

    Ok, that helps. If the tags in your input file are guaranteed to be unique, it's easy. Put them in a hash with the frequencies as the values, and then sort on the values. In this example, %tags is the hash that stores the tags and their corresponding values, and then it's sorted on the values numerically, largest to smallest. The sub make_unique_string() creates the unique key for your output file from the tag and freq.

    my %tags; while(<$input_file_descriptor>){ # do stuff to skip headers and blank lines chomp; my( $tag, $freq ) = split /\s+/; $tags{$tag} = $freq; } for my $tag (sort { $tags{$b} <=> $tags{$a} } keys %tags ){ my $freq = $tags{$tag}; # to clarify things below my $unique_string = make_unique_string($tag, $freq); print ">$unique_string\t$tag\t$freq\n"; }
      HI Aaron,

      Sorry, I didn't see your complete example.