in reply to Re^2: Sorting issue
in thread Sorting issue
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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Sorting issue
by bluray (Sexton) on Nov 05, 2011 at 03:10 UTC |