Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: String sorting in Perl

by Laurent_R (Parson)
on Jun 04, 2014 at 18:26 UTC ( #1088714=note: print w/ replies, xml ) Need Help??


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

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.


Comment on Re^3: String sorting in Perl
Re^4: String sorting in Perl
by markdavis87 (Novice) on Jun 04, 2014 at 18:29 UTC

    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;

        Thanks! I think it should be "count_hash" instead of "hash_count", though. Before I read your post, I worked out this solution:

        #!/usr/bin/perl -w use strict; my $file = "path to my file"; open (FH, "< $file") or die "Can't open $file for read: $!"; my @data = <FH>; close FH or die "Cannot close $file: $!"; my %count_hash; for my $line (@data) { $count_hash{$line} ++; } for my $line (sort { length $a <=> length $b || $count_hash{$b} <=> $c +ount_hash{$a}} keys %count_hash) { print "$line\t$count_hash{$line}\n"; }

        The only thing I see is that it seems to carry over the line break with each line, so the count ends up being on a new line, tabbed over. Is there a way to have it look for a line break and remove it before it prints>

Re^4: String sorting in Perl
by markdavis87 (Novice) on Jun 04, 2014 at 19:06 UTC

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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1088714]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2014-12-29 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls