in reply to Re: String sorting in Perl
in thread String sorting in Perl

Ah! I got it.

my @sorted_data = sort { length $a <=> length $b || $count_hash{b} <=> + $count_hash{$a} } keys %count_hash;

should be

my @sorted_data = sort { length $a <=> length $b || $count_hash{$b} <= +> + $count_hash{$a} } keys %count_hash;

We were just missing a "$" before the "b"...

Replies are listed 'Best First'.
Re^3: String sorting in Perl
by Laurent_R (Canon) on Jun 04, 2014 at 18:26 UTC
    Yes, I was going to give the answer, but you found out yourself. Sorry for the typo. I'll update my post to get it right.

      Alright, the last thing I have to ask is how I append the count to the end of each distinct line in the final output. As it stands right now, I get:

      ALPHA:D 20 letters ABCCEDFGGAACDDDEEEFG ALPHA:D 20 letters ABCCEDFFGAACDDDEEEFG ALPHA:D 20 letters ABCCEDFFGAACDDEEEEFG ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAE ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAD

      which is great! But I need to have the count of each distinct string, spaced by a tab... I'd like it to look like this:

      ALPHA:D 20 letters ABCCEDFGGAACDDDEEEFG 4 ALPHA:D 20 letters ABCCEDFFGAACDDDEEEFG 3 ALPHA:D 20 letters ABCCEDFFGAACDDEEEEFG 2 ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAE 7 ALPHA:E 24 letters ABCCEDFFGAACDDDEEEFGAGAD 5

      Is there an easy way to do this?

        Yes, quite easy. For example:
        for my $line (@sorted_data) { print "$line \t $hash_count{$line} \n"; }
        or:
        print map {"$_ \t $hash_count{$_} \n"} @sorted_data;

      Please disregard my final question. I just used "chomp" for each line, and got the results I wanted. Thanks again!