in reply to Sorting issue

Nope, I just can't do it. I tried to read your mind to determine what your input data looks like and to see how the sort was different than you want, but I just can't do it. Maybe you are too far away, or asleep, or just don't broadcast very well, but I failed. Sorry.

I suggest though that you take a hard look at your use of $tags in your foreach loop. Assigning to the loop variable seems wrong to me. There are of course times when that is the thing to do, but your code doesn't make it clear that changing the content of the loop variable is the intended behaviour as you use the same hash lookup ($tag{$tags}) in two places having changed $tags between times. If that is what you want I'd create a new variable to make it clear that that is the intent.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Sorting issue
by bluray (Sexton) on Nov 04, 2011 at 23:43 UTC
    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..."

      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.